Skip to content

Commit

Permalink
add parentheses to [let_and_return]'s suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed May 24, 2024
1 parent 05c4053 commit ecc2f43
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
6 changes: 2 additions & 4 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,9 @@ impl<'tcx> LateLintPass<'tcx> for Return {
|err| {
err.span_label(local.span, "unnecessary `let` binding");

if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
if let Some(sugg) = clippy_utils::sugg::Sugg::hir_opt(cx, initexpr) {
let mut snippet = sugg.maybe_par().to_string();
if !cx.typeck_results().expr_adjustments(retexpr).is_empty() {
if !has_enclosing_paren(&snippet) {
snippet = format!("({snippet})");
}
snippet.push_str(" as _");
}
err.multipart_suggestion(
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/crashes/ice-8850.fixed
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
fn fn_pointer_static() -> usize {
static FN: fn() -> usize = || 1;

FN() + 1
(FN() + 1)
//~^ ERROR: returning the result of a `let` binding from a block
//~| NOTE: `-D clippy::let-and-return` implied by `-D warnings`
}

fn fn_pointer_const() -> usize {
const FN: fn() -> usize = || 1;

FN() + 1
(FN() + 1)
//~^ ERROR: returning the result of a `let` binding from a block
}

Expand All @@ -24,7 +24,7 @@ fn deref_to_dyn_fn() -> usize {
}
static FN: Derefs = Derefs;

FN() + 1
(FN() + 1)
//~^ ERROR: returning the result of a `let` binding from a block
}

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/crashes/ice-8850.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | res
help: return the expression directly
|
LL ~
LL ~ FN() + 1
LL ~ (FN() + 1)
|

error: returning the result of a `let` binding from a block
Expand All @@ -25,7 +25,7 @@ LL | res
help: return the expression directly
|
LL ~
LL ~ FN() + 1
LL ~ (FN() + 1)
|

error: returning the result of a `let` binding from a block
Expand All @@ -39,7 +39,7 @@ LL | res
help: return the expression directly
|
LL ~
LL ~ FN() + 1
LL ~ (FN() + 1)
|

error: aborting due to 3 previous errors
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/let_and_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ mod issue_5729 {
impl<T: Foo + 'static> FooStorage for FooStorageImpl<T> {
fn foo_cloned(&self) -> Arc<dyn Foo> {

(Arc::clone(&self.foo)) as _
Arc::clone(&self.foo) as _
//~^ ERROR: returning the result of a `let` binding from a block
}
}
Expand Down Expand Up @@ -210,4 +210,10 @@ fn issue9150() -> usize {
x
}

fn issue12801() -> String {

(if true { "a".to_string() } else { "b".to_string() } + "c")
//~^ ERROR: returning the result of a `let` binding from a block
}

fn main() {}
6 changes: 6 additions & 0 deletions tests/ui/let_and_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,10 @@ fn issue9150() -> usize {
x
}

fn issue12801() -> String {
let s = if true { "a".to_string() } else { "b".to_string() } + "c";
s
//~^ ERROR: returning the result of a `let` binding from a block
}

fn main() {}
18 changes: 16 additions & 2 deletions tests/ui/let_and_return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ LL | clone
help: return the expression directly
|
LL ~
LL ~ (Arc::clone(&self.foo)) as _
LL ~ Arc::clone(&self.foo) as _
|

error: returning the result of a `let` binding from a block
Expand All @@ -78,5 +78,19 @@ LL + E::B(x) => x,
LL + }) as _
|

error: aborting due to 5 previous errors
error: returning the result of a `let` binding from a block
--> tests/ui/let_and_return.rs:215:5
|
LL | let s = if true { "a".to_string() } else { "b".to_string() } + "c";
| ------------------------------------------------------------------- unnecessary `let` binding
LL | s
| ^
|
help: return the expression directly
|
LL ~
LL ~ (if true { "a".to_string() } else { "b".to_string() } + "c")
|

error: aborting due to 6 previous errors

0 comments on commit ecc2f43

Please sign in to comment.