Skip to content

Commit 0da4dab

Browse files
committed
Auto merge of #11584 - koka831:fix/11335, r=blyxyas
let_and_return: Wrap with parenthesis if necessary - fixes #11335 changelog: [`let_and_return`]: Wrap suggestion with parenthesis if necessary r? `@Centri3`
2 parents 7ce6e0d + f4a8b12 commit 0da4dab

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

clippy_lints/src/returns.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
22
use clippy_utils::source::{snippet_opt, snippet_with_context};
3+
use clippy_utils::sugg::has_enclosing_paren;
34
use clippy_utils::visitors::{for_each_expr_with_closures, Descend};
45
use clippy_utils::{fn_def_id, is_from_proc_macro, path_to_local_id, span_find_starting_semi};
56
use core::ops::ControlFlow;
@@ -213,6 +214,9 @@ impl<'tcx> LateLintPass<'tcx> for Return {
213214

214215
if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
215216
if !cx.typeck_results().expr_adjustments(retexpr).is_empty() {
217+
if !has_enclosing_paren(&snippet) {
218+
snippet = format!("({snippet})");
219+
}
216220
snippet.push_str(" as _");
217221
}
218222
err.multipart_suggestion(

tests/ui/let_and_return.fixed

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,26 @@ mod issue_5729 {
168168
impl<T: Foo + 'static> FooStorage for FooStorageImpl<T> {
169169
fn foo_cloned(&self) -> Arc<dyn Foo> {
170170

171-
Arc::clone(&self.foo) as _
171+
(Arc::clone(&self.foo)) as _
172+
//~^ ERROR: returning the result of a `let` binding from a block
173+
}
174+
}
175+
}
176+
177+
mod issue_11335 {
178+
pub enum E<T> {
179+
A(T),
180+
B(T),
181+
}
182+
183+
impl<T> E<T> {
184+
pub fn inner(&self) -> &T {
185+
186+
187+
(match self {
188+
E::A(x) => x,
189+
E::B(x) => x,
190+
}) as _
172191
//~^ ERROR: returning the result of a `let` binding from a block
173192
}
174193
}

tests/ui/let_and_return.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,25 @@ mod issue_5729 {
174174
}
175175
}
176176

177+
mod issue_11335 {
178+
pub enum E<T> {
179+
A(T),
180+
B(T),
181+
}
182+
183+
impl<T> E<T> {
184+
pub fn inner(&self) -> &T {
185+
let result = match self {
186+
E::A(x) => x,
187+
E::B(x) => x,
188+
};
189+
190+
result
191+
//~^ ERROR: returning the result of a `let` binding from a block
192+
}
193+
}
194+
}
195+
177196
// https://github.com/rust-lang/rust-clippy/issues/11167
178197
macro_rules! fn_in_macro {
179198
($b:block) => {

tests/ui/let_and_return.stderr

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,30 @@ LL | clone
5353
help: return the expression directly
5454
|
5555
LL ~
56-
LL ~ Arc::clone(&self.foo) as _
56+
LL ~ (Arc::clone(&self.foo)) as _
5757
|
5858

59-
error: aborting due to 4 previous errors
59+
error: returning the result of a `let` binding from a block
60+
--> $DIR/let_and_return.rs:190:13
61+
|
62+
LL | / let result = match self {
63+
LL | | E::A(x) => x,
64+
LL | | E::B(x) => x,
65+
LL | | };
66+
| |______________- unnecessary `let` binding
67+
LL |
68+
LL | result
69+
| ^^^^^^
70+
|
71+
help: return the expression directly
72+
|
73+
LL ~
74+
LL |
75+
LL ~ (match self {
76+
LL + E::A(x) => x,
77+
LL + E::B(x) => x,
78+
LL + }) as _
79+
|
80+
81+
error: aborting due to 5 previous errors
6082

0 commit comments

Comments
 (0)