Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions clippy_lints/src/matches/match_like_matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::REDUNDANT_PATTERN_MATCHING;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::{is_lint_allowed, is_wild, span_contains_comment};
use rustc_ast::LitKind;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -43,17 +43,15 @@ pub(crate) fn check_if_let<'tcx>(
{
ex_new = ex_inner;
}

let (snippet, _) = snippet_with_context(cx, ex_new.span, expr.span.ctxt(), "..", &mut applicability);
span_lint_and_sugg(
cx,
MATCH_LIKE_MATCHES_MACRO,
expr.span,
"if let .. else expression looks like `matches!` macro",
"try",
format!(
"{}matches!({}, {pat})",
if b0 { "" } else { "!" },
snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
),
format!("{}matches!({snippet}, {pat})", if b0 { "" } else { "!" },),
applicability,
);
}
Expand Down Expand Up @@ -169,17 +167,15 @@ pub(super) fn check_match<'tcx>(
{
ex_new = ex_inner;
}

let (snippet, _) = snippet_with_context(cx, ex_new.span, e.span.ctxt(), "..", &mut applicability);
span_lint_and_sugg(
cx,
MATCH_LIKE_MATCHES_MACRO,
e.span,
"match expression looks like `matches!` macro",
"try",
format!(
"{}matches!({}, {pat_and_guard})",
if b0 { "" } else { "!" },
snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
),
format!("{}matches!({snippet}, {pat_and_guard})", if b0 { "" } else { "!" }),
applicability,
);
true
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/match_like_matches_macro.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![warn(clippy::match_like_matches_macro)]
#![allow(
unreachable_patterns,
irrefutable_let_patterns,
clippy::equatable_if_let,
clippy::needless_borrowed_reference,
clippy::redundant_guards
Expand Down Expand Up @@ -223,3 +224,24 @@ fn msrv_1_42() {
let _y = matches!(Some(5), Some(0));
//~^^^^ match_like_matches_macro
}

fn issue16015<T: 'static, U: 'static>() -> bool {
use std::any::{TypeId, type_name};
pub struct GetTypeId<T>(T);

impl<T: 'static> GetTypeId<T> {
pub const VALUE: TypeId = TypeId::of::<T>();
}

macro_rules! typeid {
($t:ty) => {
GetTypeId::<$t>::VALUE
};
}

matches!(typeid!(T), _);
//~^^^^ match_like_matches_macro

matches!(typeid!(U), _)
//~^ match_like_matches_macro
}
25 changes: 25 additions & 0 deletions tests/ui/match_like_matches_macro.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![warn(clippy::match_like_matches_macro)]
#![allow(
unreachable_patterns,
irrefutable_let_patterns,
clippy::equatable_if_let,
clippy::needless_borrowed_reference,
clippy::redundant_guards
Expand Down Expand Up @@ -267,3 +268,27 @@ fn msrv_1_42() {
};
//~^^^^ match_like_matches_macro
}

fn issue16015<T: 'static, U: 'static>() -> bool {
use std::any::{TypeId, type_name};
pub struct GetTypeId<T>(T);

impl<T: 'static> GetTypeId<T> {
pub const VALUE: TypeId = TypeId::of::<T>();
}

macro_rules! typeid {
($t:ty) => {
GetTypeId::<$t>::VALUE
};
}

match typeid!(T) {
_ => true,
_ => false,
};
//~^^^^ match_like_matches_macro

if let _ = typeid!(U) { true } else { false }
//~^ match_like_matches_macro
}
45 changes: 30 additions & 15 deletions tests/ui/match_like_matches_macro.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:13:14
--> tests/ui/match_like_matches_macro.rs:14:14
|
LL | let _y = match x {
| ______________^
Expand All @@ -12,7 +12,7 @@ LL | | };
= help: to override `-D warnings` add `#[allow(clippy::match_like_matches_macro)]`

error: redundant pattern matching, consider using `is_some()`
--> tests/ui/match_like_matches_macro.rs:20:14
--> tests/ui/match_like_matches_macro.rs:21:14
|
LL | let _w = match x {
| ______________^
Expand All @@ -25,7 +25,7 @@ LL | | };
= help: to override `-D warnings` add `#[allow(clippy::redundant_pattern_matching)]`

error: redundant pattern matching, consider using `is_none()`
--> tests/ui/match_like_matches_macro.rs:27:14
--> tests/ui/match_like_matches_macro.rs:28:14
|
LL | let _z = match x {
| ______________^
Expand All @@ -35,7 +35,7 @@ LL | | };
| |_____^ help: try: `x.is_none()`

error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:34:15
--> tests/ui/match_like_matches_macro.rs:35:15
|
LL | let _zz = match x {
| _______________^
Expand All @@ -45,13 +45,13 @@ LL | | };
| |_____^ help: try: `!matches!(x, Some(r) if r == 0)`

error: if let .. else expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:41:16
--> tests/ui/match_like_matches_macro.rs:42:16
|
LL | let _zzz = if let Some(5) = x { true } else { false };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(x, Some(5))`

error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:66:20
--> tests/ui/match_like_matches_macro.rs:67:20
|
LL | let _ans = match x {
| ____________________^
Expand All @@ -62,7 +62,7 @@ LL | | };
| |_________^ help: try: `matches!(x, E::A(_) | E::B(_))`

error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:77:20
--> tests/ui/match_like_matches_macro.rs:78:20
|
LL | let _ans = match x {
| ____________________^
Expand All @@ -74,7 +74,7 @@ LL | | };
| |_________^ help: try: `matches!(x, E::A(_) | E::B(_))`

error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:88:20
--> tests/ui/match_like_matches_macro.rs:89:20
|
LL | let _ans = match x {
| ____________________^
Expand All @@ -85,7 +85,7 @@ LL | | };
| |_________^ help: try: `!matches!(x, E::B(_) | E::C)`

error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:149:18
--> tests/ui/match_like_matches_macro.rs:150:18
|
LL | let _z = match &z {
| __________________^
Expand All @@ -95,7 +95,7 @@ LL | | };
| |_________^ help: try: `matches!(z, Some(3))`

error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:159:18
--> tests/ui/match_like_matches_macro.rs:160:18
|
LL | let _z = match &z {
| __________________^
Expand All @@ -105,7 +105,7 @@ LL | | };
| |_________^ help: try: `matches!(&z, Some(3))`

error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:177:21
--> tests/ui/match_like_matches_macro.rs:178:21
|
LL | let _ = match &z {
| _____________________^
Expand All @@ -115,7 +115,7 @@ LL | | };
| |_____________^ help: try: `matches!(&z, AnEnum::X)`

error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:192:20
--> tests/ui/match_like_matches_macro.rs:193:20
|
LL | let _res = match &val {
| ____________________^
Expand All @@ -125,7 +125,7 @@ LL | | };
| |_________^ help: try: `matches!(&val, &Some(ref _a))`

error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:205:20
--> tests/ui/match_like_matches_macro.rs:206:20
|
LL | let _res = match &val {
| ____________________^
Expand All @@ -135,7 +135,7 @@ LL | | };
| |_________^ help: try: `matches!(&val, &Some(ref _a))`

error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:264:14
--> tests/ui/match_like_matches_macro.rs:265:14
|
LL | let _y = match Some(5) {
| ______________^
Expand All @@ -144,5 +144,20 @@ LL | | _ => false,
LL | | };
| |_____^ help: try: `matches!(Some(5), Some(0))`

error: aborting due to 14 previous errors
error: match expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:286:5
|
LL | / match typeid!(T) {
LL | | _ => true,
LL | | _ => false,
LL | | };
| |_____^ help: try: `matches!(typeid!(T), _)`

error: if let .. else expression looks like `matches!` macro
--> tests/ui/match_like_matches_macro.rs:292:5
|
LL | if let _ = typeid!(U) { true } else { false }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(typeid!(U), _)`

error: aborting due to 16 previous errors

Loading