diff options
| author | bors <bors@rust-lang.org> | 2023-10-26 14:20:13 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-10-26 14:20:13 +0000 |
| commit | 0da4dab720c767826e057c17a2adbb9da1dde78e (patch) | |
| tree | 91d7c54465d3d0c243c504e67d7d45ea6a4f7ce0 | |
| parent | 7ce6e0d85324d4a128b146593884b6dad50ff996 (diff) | |
| parent | f4a8b12ed503ce998cc44320f5cd902e837b785f (diff) | |
| download | rust-0da4dab720c767826e057c17a2adbb9da1dde78e.tar.gz rust-0da4dab720c767826e057c17a2adbb9da1dde78e.zip | |
Auto merge of #11584 - koka831:fix/11335, r=blyxyas
let_and_return: Wrap with parenthesis if necessary - fixes https://github.com/rust-lang/rust-clippy/issues/11335 changelog: [`let_and_return`]: Wrap suggestion with parenthesis if necessary r? `@Centri3`
| -rw-r--r-- | clippy_lints/src/returns.rs | 4 | ||||
| -rw-r--r-- | tests/ui/let_and_return.fixed | 21 | ||||
| -rw-r--r-- | tests/ui/let_and_return.rs | 19 | ||||
| -rw-r--r-- | tests/ui/let_and_return.stderr | 26 |
4 files changed, 67 insertions, 3 deletions
diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 11acc3c2d62..72235a954ba 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -1,5 +1,6 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then}; use clippy_utils::source::{snippet_opt, snippet_with_context}; +use clippy_utils::sugg::has_enclosing_paren; use clippy_utils::visitors::{for_each_expr_with_closures, Descend}; use clippy_utils::{fn_def_id, is_from_proc_macro, path_to_local_id, span_find_starting_semi}; use core::ops::ControlFlow; @@ -213,6 +214,9 @@ impl<'tcx> LateLintPass<'tcx> for Return { if let Some(mut snippet) = snippet_opt(cx, initexpr.span) { if !cx.typeck_results().expr_adjustments(retexpr).is_empty() { + if !has_enclosing_paren(&snippet) { + snippet = format!("({snippet})"); + } snippet.push_str(" as _"); } err.multipart_suggestion( diff --git a/tests/ui/let_and_return.fixed b/tests/ui/let_and_return.fixed index 88b8ae67375..b5584fcde8c 100644 --- a/tests/ui/let_and_return.fixed +++ b/tests/ui/let_and_return.fixed @@ -168,7 +168,26 @@ 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 + } + } +} + +mod issue_11335 { + pub enum E<T> { + A(T), + B(T), + } + + impl<T> E<T> { + pub fn inner(&self) -> &T { + + + (match self { + E::A(x) => x, + E::B(x) => x, + }) as _ //~^ ERROR: returning the result of a `let` binding from a block } } diff --git a/tests/ui/let_and_return.rs b/tests/ui/let_and_return.rs index f366842c5d7..f13c7c4e203 100644 --- a/tests/ui/let_and_return.rs +++ b/tests/ui/let_and_return.rs @@ -174,6 +174,25 @@ mod issue_5729 { } } +mod issue_11335 { + pub enum E<T> { + A(T), + B(T), + } + + impl<T> E<T> { + pub fn inner(&self) -> &T { + let result = match self { + E::A(x) => x, + E::B(x) => x, + }; + + result + //~^ ERROR: returning the result of a `let` binding from a block + } + } +} + // https://github.com/rust-lang/rust-clippy/issues/11167 macro_rules! fn_in_macro { ($b:block) => { diff --git a/tests/ui/let_and_return.stderr b/tests/ui/let_and_return.stderr index c09c2b32aad..fe60072d13f 100644 --- a/tests/ui/let_and_return.stderr +++ b/tests/ui/let_and_return.stderr @@ -53,8 +53,30 @@ LL | clone help: return the expression directly | LL ~ -LL ~ Arc::clone(&self.foo) as _ +LL ~ (Arc::clone(&self.foo)) as _ | -error: aborting due to 4 previous errors +error: returning the result of a `let` binding from a block + --> $DIR/let_and_return.rs:190:13 + | +LL | / let result = match self { +LL | | E::A(x) => x, +LL | | E::B(x) => x, +LL | | }; + | |______________- unnecessary `let` binding +LL | +LL | result + | ^^^^^^ + | +help: return the expression directly + | +LL ~ +LL | +LL ~ (match self { +LL + E::A(x) => x, +LL + E::B(x) => x, +LL + }) as _ + | + +error: aborting due to 5 previous errors |
