Skip to content

fix: ignore pattern_type_mismatch when external macro owns the match #15306

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

Merged
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
6 changes: 6 additions & 0 deletions clippy_lints/src/pattern_type_mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {

fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if let ExprKind::Match(_, arms, _) = expr.kind {
// if the match is generated by an external macro, the writer does not control
// how the scrutinee (`match &scrutiny { ... }`) is matched
if expr.span.in_external_macro(cx.sess().source_map()) {
return;
}

for arm in arms {
let pat = &arm.pat;
if apply_lint(cx, pat, DerefPossible::Possible) {
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/pattern_type_mismatch/auxiliary/external.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! **FAKE** external macro crate.

#[macro_export]
macro_rules! macro_with_match {
( $p:pat ) => {
let something = ();

match &something {
$p => true,
_ => false,
}
};
}
9 changes: 9 additions & 0 deletions tests/ui/pattern_type_mismatch/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
clippy::single_match
)]

//@aux-build:external.rs
use external::macro_with_match;

fn main() {}

fn syntax_match() {
Expand Down Expand Up @@ -159,3 +162,9 @@ fn macro_expansion() {
let value = &Some(23);
matching_macro!(value);
}

fn external_macro_expansion() {
macro_with_match! {
()
};
}
18 changes: 9 additions & 9 deletions tests/ui/pattern_type_mismatch/syntax.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: type of pattern does not match the expression type
--> tests/ui/pattern_type_mismatch/syntax.rs:16:9
--> tests/ui/pattern_type_mismatch/syntax.rs:19:9
|
LL | Some(_) => (),
| ^^^^^^^
Expand All @@ -9,63 +9,63 @@ LL | Some(_) => (),
= help: to override `-D warnings` add `#[allow(clippy::pattern_type_mismatch)]`

error: type of pattern does not match the expression type
--> tests/ui/pattern_type_mismatch/syntax.rs:36:12
--> tests/ui/pattern_type_mismatch/syntax.rs:39:12
|
LL | if let Some(_) = ref_value {}
| ^^^^^^^
|
= help: use `*` to dereference the match expression or explicitly match against a `&_` pattern and adjust the enclosed variable bindings

error: type of pattern does not match the expression type
--> tests/ui/pattern_type_mismatch/syntax.rs:48:15
--> tests/ui/pattern_type_mismatch/syntax.rs:51:15
|
LL | while let Some(_) = ref_value {
| ^^^^^^^
|
= help: use `*` to dereference the match expression or explicitly match against a `&_` pattern and adjust the enclosed variable bindings

error: type of pattern does not match the expression type
--> tests/ui/pattern_type_mismatch/syntax.rs:68:9
--> tests/ui/pattern_type_mismatch/syntax.rs:71:9
|
LL | for (_a, _b) in slice.iter() {}
| ^^^^^^^^
|
= help: explicitly match against a `&_` pattern and adjust the enclosed variable bindings

error: type of pattern does not match the expression type
--> tests/ui/pattern_type_mismatch/syntax.rs:79:9
--> tests/ui/pattern_type_mismatch/syntax.rs:82:9
|
LL | let (_n, _m) = ref_value;
| ^^^^^^^^
|
= help: use `*` to dereference the match expression or explicitly match against a `&_` pattern and adjust the enclosed variable bindings

error: type of pattern does not match the expression type
--> tests/ui/pattern_type_mismatch/syntax.rs:89:12
--> tests/ui/pattern_type_mismatch/syntax.rs:92:12
|
LL | fn foo((_a, _b): &(i32, i32)) {}
| ^^^^^^^^
|
= help: explicitly match against a `&_` pattern and adjust the enclosed variable bindings

error: type of pattern does not match the expression type
--> tests/ui/pattern_type_mismatch/syntax.rs:104:10
--> tests/ui/pattern_type_mismatch/syntax.rs:107:10
|
LL | foo(|(_a, _b)| ());
| ^^^^^^^^
|
= help: explicitly match against a `&_` pattern and adjust the enclosed variable bindings

error: type of pattern does not match the expression type
--> tests/ui/pattern_type_mismatch/syntax.rs:121:9
--> tests/ui/pattern_type_mismatch/syntax.rs:124:9
|
LL | Some(_) => (),
| ^^^^^^^
|
= help: use `*` to dereference the match expression or explicitly match against a `&_` pattern and adjust the enclosed variable bindings

error: type of pattern does not match the expression type
--> tests/ui/pattern_type_mismatch/syntax.rs:142:17
--> tests/ui/pattern_type_mismatch/syntax.rs:145:17
|
LL | Some(_) => (),
| ^^^^^^^
Expand Down