Skip to content

question_mark inside try blocks #8628

@Kyuuhachi

Description

@Kyuuhachi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions