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

Activity

added
C-bugCategory: Clippy is not doing the correct thing
I-false-positiveIssue: The lint was triggered on code it shouldn't have
on Apr 4, 2022
added
I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
on Apr 4, 2022
added a commit that references this issue on Jun 28, 2023
10ce1a6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @Kyuuhachi@rustbot

      Issue actions

        `question_mark` inside `try` blocks · Issue #8628 · rust-lang/rust-clippy