Skip to content

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

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
LikeLakers2 opened this issue Apr 22, 2023 · 2 comments
Closed

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

LikeLakers2 opened this issue Apr 22, 2023 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@LikeLakers2
Copy link

LikeLakers2 commented Apr 22, 2023

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
@LikeLakers2 LikeLakers2 added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Apr 22, 2023
@y21
Copy link
Member

y21 commented Jun 4, 2023

#10831 fixed this issue

@LikeLakers2
Copy link
Author

Closed per #10831 being merged.

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-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

No branches or pull requests

2 participants