mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 23:24:24 +00:00
Removed lintining on never type.
Abstracted repeating strings into statics.
This commit is contained in:
parent
6c067bf50e
commit
a2b63af746
3 changed files with 17 additions and 34 deletions
|
@ -42,6 +42,9 @@ declare_clippy_lint! {
|
|||
|
||||
declare_lint_pass!(ImplicitReturn => [IMPLICIT_RETURN]);
|
||||
|
||||
static LINT_BREAK: &str = "change `break` to `return` as shown";
|
||||
static LINT_RETURN: &str = "add `return` as shown";
|
||||
|
||||
fn lint(cx: &LateContext<'_, '_>, outer_span: Span, inner_span: Span, msg: &str) {
|
||||
let outer_span = span_to_outer_expn(outer_span);
|
||||
let inner_span = span_to_outer_expn(inner_span);
|
||||
|
@ -66,12 +69,12 @@ fn span_to_outer_expn(span: Span) -> Span {
|
|||
}
|
||||
}
|
||||
|
||||
fn expr_match(cx: &LateContext<'_, '_>, expr: &Expr, ret_ty_is_never: bool) {
|
||||
fn expr_match(cx: &LateContext<'_, '_>, expr: &Expr) {
|
||||
match &expr.node {
|
||||
// loops could be using `break` instead of `return`
|
||||
ExprKind::Block(block, ..) | ExprKind::Loop(block, ..) => {
|
||||
if let Some(expr) = &block.expr {
|
||||
expr_match(cx, expr, ret_ty_is_never);
|
||||
expr_match(cx, expr);
|
||||
}
|
||||
// only needed in the case of `break` with `;` at the end
|
||||
else if let Some(stmt) = block.stmts.last() {
|
||||
|
@ -81,7 +84,7 @@ fn expr_match(cx: &LateContext<'_, '_>, expr: &Expr, ret_ty_is_never: bool) {
|
|||
if let ExprKind::Break(.., break_expr) = &expr.node;
|
||||
if let Some(break_expr) = break_expr;
|
||||
then {
|
||||
lint(cx, expr.span, break_expr.span, "change `break` to `return` as shown");
|
||||
lint(cx, expr.span, break_expr.span, LINT_BREAK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -89,7 +92,7 @@ fn expr_match(cx: &LateContext<'_, '_>, expr: &Expr, ret_ty_is_never: bool) {
|
|||
// use `return` instead of `break`
|
||||
ExprKind::Break(.., break_expr) => {
|
||||
if let Some(break_expr) = break_expr {
|
||||
lint(cx, expr.span, break_expr.span, "change `break` to `return` as shown");
|
||||
lint(cx, expr.span, break_expr.span, LINT_BREAK);
|
||||
}
|
||||
},
|
||||
ExprKind::Match(.., arms, source) => {
|
||||
|
@ -102,38 +105,29 @@ fn expr_match(cx: &LateContext<'_, '_>, expr: &Expr, ret_ty_is_never: bool) {
|
|||
|
||||
if check_all_arms {
|
||||
for arm in arms {
|
||||
expr_match(cx, &arm.body, ret_ty_is_never);
|
||||
expr_match(cx, &arm.body);
|
||||
}
|
||||
} else {
|
||||
expr_match(
|
||||
cx,
|
||||
&arms.first().expect("if let doesn't have a single arm").body,
|
||||
ret_ty_is_never,
|
||||
);
|
||||
expr_match(cx, &arms.first().expect("if let doesn't have a single arm").body);
|
||||
}
|
||||
},
|
||||
// skip if it already has a return statement
|
||||
ExprKind::Ret(..) => (),
|
||||
// make sure it's not a call that panics unless we intend to return a panic
|
||||
// make sure it's not a call that panics
|
||||
ExprKind::Call(expr, ..) => {
|
||||
if_chain! {
|
||||
if let ExprKind::Path(qpath) = &expr.node;
|
||||
if let Some(path_def_id) = resolve_node(cx, qpath, expr.hir_id).opt_def_id();
|
||||
if match_def_path(cx, path_def_id, &BEGIN_PANIC) ||
|
||||
match_def_path(cx, path_def_id, &BEGIN_PANIC_FMT);
|
||||
then {
|
||||
// only put `return` on panics if the return type of the function/closure is a panic
|
||||
if ret_ty_is_never {
|
||||
lint(cx, expr.span, expr.span, "add `return` as shown")
|
||||
}
|
||||
}
|
||||
then { }
|
||||
else {
|
||||
lint(cx, expr.span, expr.span, "add `return` as shown")
|
||||
lint(cx, expr.span, expr.span, LINT_RETURN)
|
||||
}
|
||||
}
|
||||
},
|
||||
// everything else is missing `return`
|
||||
_ => lint(cx, expr.span, expr.span, "add `return` as shown"),
|
||||
_ => lint(cx, expr.span, expr.span, LINT_RETURN),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -149,12 +143,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitReturn {
|
|||
) {
|
||||
let def_id = cx.tcx.hir().body_owner_def_id(body.id());
|
||||
let mir = cx.tcx.optimized_mir(def_id);
|
||||
let ret_ty = mir.return_ty();
|
||||
|
||||
// checking return type through MIR, HIR is not able to determine inferred closure return types
|
||||
// make sure it's not a macro
|
||||
if !ret_ty.is_unit() && !in_macro_or_desugar(span) {
|
||||
expr_match(cx, &body.value, ret_ty.is_never());
|
||||
if !mir.return_ty().is_unit() && !in_macro_or_desugar(span) {
|
||||
expr_match(cx, &body.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,10 +77,6 @@ fn test_closure() {
|
|||
let _ = || true;
|
||||
}
|
||||
|
||||
fn test_return_never() -> ! {
|
||||
panic!()
|
||||
}
|
||||
|
||||
fn test_panic() -> bool {
|
||||
panic!()
|
||||
}
|
||||
|
|
|
@ -61,16 +61,10 @@ LL | let _ = || true;
|
|||
| ^^^^ help: add `return` as shown: `return true`
|
||||
|
||||
error: missing return statement
|
||||
--> $DIR/implicit_return.rs:81:5
|
||||
|
|
||||
LL | panic!()
|
||||
| ^^^^^^^^ help: add `return` as shown: `return panic!()`
|
||||
|
||||
error: missing return statement
|
||||
--> $DIR/implicit_return.rs:89:5
|
||||
--> $DIR/implicit_return.rs:85:5
|
||||
|
|
||||
LL | format!("test {}", "test")
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `return` as shown: `return format!("test {}", "test")`
|
||||
|
||||
error: aborting due to 12 previous errors
|
||||
error: aborting due to 11 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue