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

Wrong suggestion for redundant_closure_call when body contains return, ? or try!. #1684

Closed
kennytm opened this issue Apr 16, 2017 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@kennytm
Copy link
Member

kennytm commented Apr 16, 2017

Test case:

fn main() {
	let e = (|| -> Result<i32, i32> {
		Err(3)?;
		Ok(1)
	})();
	assert_eq(e, Err(3));
}

Clippy will suggest

warning: Try not to call a closure in the expression where it is declared.
 --> src/main.rs:2:10
  |
2 |   	let e = (|| -> Result<i32, i32> {
  |  __________^ starting here...
3 | | 		Err(3)?;
4 | | 		Ok(1)
5 | | 	})();
  | |_____^ ...ending here
  |
  = note: #[warn(redundant_closure_call)] on by default
help: Try doing something like: 
  | 	let e = {
  | 		Err(3)?;
  | 		Ok(1)
  | 	};
  = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#redundant_closure_call

But the suggestion is wrong, because the ? will propagate the error outside the block. Similar problem for early-return.

Clippy should avoid generating redundant_closure_call if ? is present in the body.

After catch expression is stablized (rust-lang/rust#31436, #![feature(catch_expr)]), the lint could be re-enabled when the body only contains ?. The suggestion should be let e = catch { … } though.

Not sure about early-return (including try!), maybe just turn off the code suggestion, and recommend the coder to restructure the expression.


$ rustc -vV
rustc 1.18.0-nightly (28a742997 2017-04-13)
binary: rustc
commit-hash: 28a74299778cdad4ea999e4ee8f8c1ef793338bd
commit-date: 2017-04-13
host: x86_64-apple-darwin
release: 1.18.0-nightly
LLVM version: 3.9

$ cargo -vV
cargo 0.19.0-nightly (c416fb60b 2017-04-05)
release: 0.19.0
commit-hash: c416fb60b11ecfd2a1ba0fb8567c9a92590b5d28
commit-date: 2017-04-05

$ cargo clippy -V
0.0.123
@Manishearth Manishearth added the C-bug Category: Clippy is not doing the correct thing label Jun 21, 2018
bors bot added a commit that referenced this issue Nov 14, 2018
3427: Fix wrong suggestion for `redundant_closure_call` r=oli-obk a=mikerite

Fixes #1684

Co-authored-by: Michael Wright <mikerite@lavabit.com>
@bors bors bot closed this as completed in #3427 Nov 14, 2018
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
Projects
None yet
Development

No branches or pull requests

2 participants