Skip to content

question_mark inside try blocks #8628

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

Closed
Kyuuhachi opened this issue Apr 4, 2022 · 0 comments · Fixed by #11001
Closed

question_mark inside try blocks #8628

Kyuuhachi opened this issue Apr 4, 2022 · 0 comments · Fixed by #11001
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

@Kyuuhachi
Copy link

Summary

The rewrites suggested by question_mark do not work correctly inside try blocks (unstable with #![feature(try_blocks)]), since ? exits the try block rather than the function.

I don't know what the policy is for unstable features, so just close this if it's irrelevant.

Lint Name

question_mark

Reproducer

With the following function,

fn foo1(a: Option<u32>) -> Option<u32> {
	let b: Option<u32> = try {
		if a.is_none() {
			return None;
		}
		32
	};
	b.or(Some(128))
}

Clippy suggests rewriting it as

fn foo2(a: Option<u32>) -> Option<u32> {
	let b: Option<u32> = try {
		a?;
		32
	};
	b.or(Some(128))
}

However, foo1(None) gives None, while foo2(None) gives Some(128), which is not the same.

Version

rustc 1.61.0-nightly (9f4dc0b4d 2022-03-23)
binary: rustc
commit-hash: 9f4dc0b4db892271cd0dada6e072775b5b5d6b1e
commit-date: 2022-03-23
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@Kyuuhachi Kyuuhachi 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 Apr 4, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 4, 2022
@bors bors closed this as completed in 10ce1a6 Jun 28, 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

Successfully merging a pull request may close this issue.

2 participants