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

clippy::unnecessary_lazy_evaluations produces a suggestion that would cause a divide-by-zero panic, despite the original code not causing such #11722

Closed
LikeLakers2 opened this issue Oct 27, 2023 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@LikeLakers2
Copy link

LikeLakers2 commented Oct 27, 2023

Summary

As the title says, one of Clippy's lints has produced a suggestion that would cause a divide-by-zero panic. The code and clippy's suggestion are listed below, under the Reproducer header.

Lint Name

clippy::unnecessary_lazy_evaluations

Reproducer

I tried this code:

fn clippy_lint_error(num1: usize, num2: usize) -> Option<(usize, usize)> {
	(num2 != 0).then(|| (
		num1 / num2,
		num1 % num2
	))
}

I saw this happen:

warning: unnecessary closure used with `bool::then`
  --> src\snippet.rs:23:3
   |
23 | /         (num2 != 0).then(|| (
24 | |             num1 / num2,
25 | |             num1 % num2
26 | |         ))
   | |__________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
   = note: `#[warn(clippy::unnecessary_lazy_evaluations)]` on by default
help: use `then_some(..)` instead
   |
23 ~         (num2 != 0).then_some((
24 ~             num1 / num2,
25 ~             num1 % num2
26 ~         ))
   |

I expected to see this happen:

No lint triggering at all - or alternative code that would still avoid the same panic, like:

// Note: This code does not trigger Clippy
fn clippy_lint_error_fixed(num1: usize, num2: usize) -> Option<(usize, usize)> {
	if num2 != 0 {
		Some((
			num1 / num2,
			num1 % num2
		))
	} else {
		None
	}
}

Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-pc-windows-msvc
release: 1.73.0
LLVM version: 17.0.2

Additional Labels

@rustbot label +I-suggestion-causes-error

@LikeLakers2 LikeLakers2 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Oct 27, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Oct 27, 2023
@LikeLakers2
Copy link
Author

LikeLakers2 commented Oct 27, 2023

Closing this, since I found an old issue reporting the same thing while double-checking if this was a duplicate or not: #9422

@LikeLakers2 LikeLakers2 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 27, 2023
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 I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

2 participants