Skip to content

clippy::redundant_pattern_matching does not check for matches!() #10694

@LikeLakers2

Description

@LikeLakers2

Summary

Uses of the matches!() macro - where the first argument is/returns a Result or Option, and the second argument only matches the variant but not the value inside the variant (meaning Ok(_), Err(_), Some(_), and None) - do not trigger clippy::redundant_pattern_matching, despite being equivalent to code that would trigger clippy::redundant_pattern_matching.

Lint Name

clippy::redundant_pattern_matching

Reproducer

I tried this code:

let is_ok = matches!(res_func("good input"), Ok(_));
let is_err = matches!(res_func("bad input"), Err(_));

let is_some = matches!(opt_func("good input"), Some(_));
let is_none = matches!(opt_func("bad input"), None);

which gets expanded to the functional equivalent of:

let is_ok = match res_func("good input") {
	Ok(_) => true,
	Err(_) => false,
};
let is_err = match res_func("bad input") {
	Ok(_) => false,
	Err(_) => true,
};

let is_some = match opt_func("good input") {
	Some(_) => true,
	None => false,
};
let is_none = match opt_func("bad input") {
	Some(_) => false,
	None => true,
};

// Fun (but unrelated) fact: If you change the `Err(_)` or `None` arms above to
// `_`, it starts triggering `clippy::match_like_matches_macro` instead of
// `clippy::redundant_pattern_matching`. Weird, huh?

which should trigger clippy::redundant_pattern_matching, and suggest the code be changed to the following:

let is_ok = res_func("good input").is_ok();
let is_err = res_func("bad input").is_err();

let is_some = opt_func("good input").is_some();
let is_none = opt_func("bad input").is_none();

Instead, it did not trigger at all.

Version

rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: x86_64-pc-windows-msvc
release: 1.69.0
LLVM version: 15.0.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions