Fix #[expect]
for clippy::implicit_return
This commit is contained in:
parent
b2976980b5
commit
54ad99b6e5
@ -1,5 +1,5 @@
|
||||
use clippy_utils::{
|
||||
diagnostics::span_lint_and_sugg,
|
||||
diagnostics::span_lint_hir_and_then,
|
||||
get_async_fn_body, is_async_fn,
|
||||
source::{snippet_with_applicability, snippet_with_context, walk_span_to_context},
|
||||
visitors::expr_visitor_no_bodies,
|
||||
@ -43,31 +43,38 @@ declare_clippy_lint! {
|
||||
|
||||
declare_lint_pass!(ImplicitReturn => [IMPLICIT_RETURN]);
|
||||
|
||||
fn lint_return(cx: &LateContext<'_>, span: Span) {
|
||||
fn lint_return(cx: &LateContext<'_>, emission_place: HirId, span: Span) {
|
||||
let mut app = Applicability::MachineApplicable;
|
||||
let snip = snippet_with_applicability(cx, span, "..", &mut app);
|
||||
span_lint_and_sugg(
|
||||
span_lint_hir_and_then(
|
||||
cx,
|
||||
IMPLICIT_RETURN,
|
||||
emission_place,
|
||||
span,
|
||||
"missing `return` statement",
|
||||
"add `return` as shown",
|
||||
format!("return {}", snip),
|
||||
app,
|
||||
|diag| {
|
||||
diag.span_suggestion(span, "add `return` as shown", format!("return {}", snip), app);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn lint_break(cx: &LateContext<'_>, break_span: Span, expr_span: Span) {
|
||||
fn lint_break(cx: &LateContext<'_>, emission_place: HirId, break_span: Span, expr_span: Span) {
|
||||
let mut app = Applicability::MachineApplicable;
|
||||
let snip = snippet_with_context(cx, expr_span, break_span.ctxt(), "..", &mut app).0;
|
||||
span_lint_and_sugg(
|
||||
span_lint_hir_and_then(
|
||||
cx,
|
||||
IMPLICIT_RETURN,
|
||||
emission_place,
|
||||
break_span,
|
||||
"missing `return` statement",
|
||||
"change `break` to `return` as shown",
|
||||
format!("return {}", snip),
|
||||
app,
|
||||
|diag| {
|
||||
diag.span_suggestion(
|
||||
break_span,
|
||||
"change `break` to `return` as shown",
|
||||
format!("return {}", snip),
|
||||
app,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@ -152,7 +159,7 @@ fn lint_implicit_returns(
|
||||
// At this point sub_expr can be `None` in async functions which either diverge, or return
|
||||
// the unit type.
|
||||
if let Some(sub_expr) = sub_expr {
|
||||
lint_break(cx, e.span, sub_expr.span);
|
||||
lint_break(cx, e.hir_id, e.span, sub_expr.span);
|
||||
}
|
||||
} else {
|
||||
// the break expression is from a macro call, add a return to the loop
|
||||
@ -166,10 +173,10 @@ fn lint_implicit_returns(
|
||||
if add_return {
|
||||
#[expect(clippy::option_if_let_else)]
|
||||
if let Some(span) = call_site_span {
|
||||
lint_return(cx, span);
|
||||
lint_return(cx, expr.hir_id, span);
|
||||
LintLocation::Parent
|
||||
} else {
|
||||
lint_return(cx, expr.span);
|
||||
lint_return(cx, expr.hir_id, expr.span);
|
||||
LintLocation::Inner
|
||||
}
|
||||
} else {
|
||||
@ -198,10 +205,10 @@ fn lint_implicit_returns(
|
||||
{
|
||||
#[expect(clippy::option_if_let_else)]
|
||||
if let Some(span) = call_site_span {
|
||||
lint_return(cx, span);
|
||||
lint_return(cx, expr.hir_id, span);
|
||||
LintLocation::Parent
|
||||
} else {
|
||||
lint_return(cx, expr.span);
|
||||
lint_return(cx, expr.hir_id, expr.span);
|
||||
LintLocation::Inner
|
||||
}
|
||||
},
|
||||
|
@ -1,5 +1,5 @@
|
||||
// run-rustfix
|
||||
|
||||
#![feature(lint_reasons)]
|
||||
#![warn(clippy::implicit_return)]
|
||||
#![allow(clippy::needless_return, clippy::needless_bool, unused, clippy::never_loop)]
|
||||
|
||||
@ -128,3 +128,13 @@ async fn foo() -> bool {
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
||||
fn check_expect() -> bool {
|
||||
if true {
|
||||
// no error!
|
||||
return true;
|
||||
}
|
||||
|
||||
#[expect(clippy::implicit_return)]
|
||||
true
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
// run-rustfix
|
||||
|
||||
#![feature(lint_reasons)]
|
||||
#![warn(clippy::implicit_return)]
|
||||
#![allow(clippy::needless_return, clippy::needless_bool, unused, clippy::never_loop)]
|
||||
|
||||
@ -128,3 +128,13 @@ async fn foo() -> bool {
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
||||
fn check_expect() -> bool {
|
||||
if true {
|
||||
// no error!
|
||||
return true;
|
||||
}
|
||||
|
||||
#[expect(clippy::implicit_return)]
|
||||
true
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user