Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let_and_return lint false positive #5729

Closed
ilyvion opened this issue Jun 18, 2020 · 0 comments · Fixed by #5946
Closed

let_and_return lint false positive #5729

ilyvion opened this issue Jun 18, 2020 · 0 comments · Fixed by #5946
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@ilyvion
Copy link

ilyvion commented Jun 18, 2020

I've encountered a false positive with the let_and_return lint. Here's a minimum example that demonstrates the issue: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=37bcec97617120eafab94ca5216bf579

If you run clippy on it, you get the following output:

warning: returning the result of a `let` binding from a block
  --> src/lib.rs:18:9
   |
17 |         let clone = Arc::clone(&self.foo);
   |         ---------------------------------- unnecessary `let` binding
18 |         clone
   |         ^^^^^
   |
   = note: `#[warn(clippy::let_and_return)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return
help: return the expression directly
   |
17 |         
18 |         Arc::clone(&self.foo)
   |

If you try to follow its advice, however, and try to return Arc::clone(&self.foo) directly, you will instead get this compilation error:

error[E0308]: mismatched types
  --> src/lib.rs:17:20
   |
13 | impl<T: Foo + 'static> FooStorage
   |      - this type parameter
...
17 |         Arc::clone(&self.foo)
   |                    ^^^^^^^^^ expected trait object `dyn Foo`, found type parameter `T`
   |
   = note: expected reference `&std::sync::Arc<dyn Foo>`
              found reference `&std::sync::Arc<T>`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
@flip1995 flip1995 added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Jun 22, 2020
bors added a commit that referenced this issue Aug 26, 2020
Fix `let_and_return` bad suggestion

Add a cast to the suggestion when the return expression has adjustments.
These adjustments are lost when the suggestion is applied.

This is similar to the problem in issue #4437.

Closes #5729

changelog: Fix `let_and_return` bad suggestion
@bors bors closed this as completed in e8d33d7 Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants