Removed lintining on never type.
Abstracted repeating strings into statics.
This commit is contained in:
parent
6c067bf50e
commit
a2b63af746
@ -42,6 +42,9 @@ declare_clippy_lint! {
|
|||||||
|
|
||||||
declare_lint_pass!(ImplicitReturn => [IMPLICIT_RETURN]);
|
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) {
|
fn lint(cx: &LateContext<'_, '_>, outer_span: Span, inner_span: Span, msg: &str) {
|
||||||
let outer_span = span_to_outer_expn(outer_span);
|
let outer_span = span_to_outer_expn(outer_span);
|
||||||
let inner_span = span_to_outer_expn(inner_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 {
|
match &expr.node {
|
||||||
// loops could be using `break` instead of `return`
|
// loops could be using `break` instead of `return`
|
||||||
ExprKind::Block(block, ..) | ExprKind::Loop(block, ..) => {
|
ExprKind::Block(block, ..) | ExprKind::Loop(block, ..) => {
|
||||||
if let Some(expr) = &block.expr {
|
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
|
// only needed in the case of `break` with `;` at the end
|
||||||
else if let Some(stmt) = block.stmts.last() {
|
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 ExprKind::Break(.., break_expr) = &expr.node;
|
||||||
if let Some(break_expr) = break_expr;
|
if let Some(break_expr) = break_expr;
|
||||||
then {
|
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`
|
// use `return` instead of `break`
|
||||||
ExprKind::Break(.., break_expr) => {
|
ExprKind::Break(.., break_expr) => {
|
||||||
if let Some(break_expr) = 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) => {
|
ExprKind::Match(.., arms, source) => {
|
||||||
@ -102,38 +105,29 @@ fn expr_match(cx: &LateContext<'_, '_>, expr: &Expr, ret_ty_is_never: bool) {
|
|||||||
|
|
||||||
if check_all_arms {
|
if check_all_arms {
|
||||||
for arm in arms {
|
for arm in arms {
|
||||||
expr_match(cx, &arm.body, ret_ty_is_never);
|
expr_match(cx, &arm.body);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
expr_match(
|
expr_match(cx, &arms.first().expect("if let doesn't have a single arm").body);
|
||||||
cx,
|
|
||||||
&arms.first().expect("if let doesn't have a single arm").body,
|
|
||||||
ret_ty_is_never,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// skip if it already has a return statement
|
// skip if it already has a return statement
|
||||||
ExprKind::Ret(..) => (),
|
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, ..) => {
|
ExprKind::Call(expr, ..) => {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let ExprKind::Path(qpath) = &expr.node;
|
if let ExprKind::Path(qpath) = &expr.node;
|
||||||
if let Some(path_def_id) = resolve_node(cx, qpath, expr.hir_id).opt_def_id();
|
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) ||
|
if match_def_path(cx, path_def_id, &BEGIN_PANIC) ||
|
||||||
match_def_path(cx, path_def_id, &BEGIN_PANIC_FMT);
|
match_def_path(cx, path_def_id, &BEGIN_PANIC_FMT);
|
||||||
then {
|
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
lint(cx, expr.span, expr.span, "add `return` as shown")
|
lint(cx, expr.span, expr.span, LINT_RETURN)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// everything else is missing `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 def_id = cx.tcx.hir().body_owner_def_id(body.id());
|
||||||
let mir = cx.tcx.optimized_mir(def_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
|
// checking return type through MIR, HIR is not able to determine inferred closure return types
|
||||||
// make sure it's not a macro
|
// make sure it's not a macro
|
||||||
if !ret_ty.is_unit() && !in_macro_or_desugar(span) {
|
if !mir.return_ty().is_unit() && !in_macro_or_desugar(span) {
|
||||||
expr_match(cx, &body.value, ret_ty.is_never());
|
expr_match(cx, &body.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,10 +77,6 @@ fn test_closure() {
|
|||||||
let _ = || true;
|
let _ = || true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_return_never() -> ! {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_panic() -> bool {
|
fn test_panic() -> bool {
|
||||||
panic!()
|
panic!()
|
||||||
}
|
}
|
||||||
|
@ -61,16 +61,10 @@ LL | let _ = || true;
|
|||||||
| ^^^^ help: add `return` as shown: `return true`
|
| ^^^^ help: add `return` as shown: `return true`
|
||||||
|
|
||||||
error: missing return statement
|
error: missing return statement
|
||||||
--> $DIR/implicit_return.rs:81:5
|
--> $DIR/implicit_return.rs:85:5
|
||||||
|
|
|
||||||
LL | panic!()
|
|
||||||
| ^^^^^^^^ help: add `return` as shown: `return panic!()`
|
|
||||||
|
|
||||||
error: missing return statement
|
|
||||||
--> $DIR/implicit_return.rs:89:5
|
|
||||||
|
|
|
|
||||||
LL | format!("test {}", "test")
|
LL | format!("test {}", "test")
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `return` as shown: `return 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 New Issue
Block a user