Skip to content

Commit 468be18

Browse files
committed
add support for unequal types
1 parent fa82547 commit 468be18

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

clippy_lints/src/matches/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod significant_drop_in_scrutinee;
2323
mod single_match;
2424
mod try_err;
2525
mod wild_in_or_pats;
26+
mod unnecessary_pattern_matching;
2627

2728
use clippy_config::msrvs::{self, Msrv};
2829
use clippy_utils::source::{snippet_opt, walk_span_to_context};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use super::super::methods::UNNECESSARY_PATTERN_MATCHING;

clippy_lints/src/methods/unnecessary_pattern_matching.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use super::UNNECESSARY_PATTERN_MATCHING;
1515
// Only checking map_or for now
1616
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def: &Expr<'_>, map: &Expr<'_>) {
1717
if let ExprKind::Lit(def_kind) = def.kind
18-
&& let recv_ty = cx.typeck_results().expr_ty(recv)
18+
&& let typeck_results = cx.typeck_results()
19+
&& let recv_ty = typeck_results.expr_ty(recv)
1920
&& (is_type_diagnostic_item(cx, recv_ty, sym::Option)
2021
|| is_type_diagnostic_item(cx, recv_ty, sym::Result))
2122
&& let Bool(def_bool) = def_kind.node
@@ -46,7 +47,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def:
4647
// expensive computation inside of it
4748
if is_local_used(cx, non_binding_location, hir_id)
4849
|| !switch_to_eager_eval(cx, non_binding_location)
49-
|| l.kind {
50+
|| typeck_results.expr_ty(l) != typeck_results.expr_ty(r) {
5051
return;
5152
};
5253
span_lint_and_sugg(

tests/ui/iter_over_hash_type.stderr

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error: iterating over unordered hash-based type
2+
--> $DIR/iter_over_hash_type.rs:13:5
3+
|
4+
LL | / for x in &hash_set {
5+
LL | | let _ = x;
6+
LL | | }
7+
| |_____^
8+
|
9+
= note: `-D clippy::iter-over-hash-type` implied by `-D warnings`
10+
= help: to override `-D warnings` add `#[allow(clippy::iter_over_hash_type)]`
11+
12+
error: iterating over unordered hash-based type
13+
--> $DIR/iter_over_hash_type.rs:16:5
14+
|
15+
LL | / for x in hash_set.iter() {
16+
LL | | let _ = x;
17+
LL | | }
18+
| |_____^
19+
20+
error: iterating over unordered hash-based type
21+
--> $DIR/iter_over_hash_type.rs:19:5
22+
|
23+
LL | / for x in hash_set {
24+
LL | | let _ = x;
25+
LL | | }
26+
| |_____^
27+
28+
error: iterating over unordered hash-based type
29+
--> $DIR/iter_over_hash_type.rs:22:5
30+
|
31+
LL | / for (x, y) in &hash_map {
32+
LL | | let _ = (x, y);
33+
LL | | }
34+
| |_____^
35+
36+
error: iterating over unordered hash-based type
37+
--> $DIR/iter_over_hash_type.rs:25:5
38+
|
39+
LL | / for x in hash_map.keys() {
40+
LL | | let _ = x;
41+
LL | | }
42+
| |_____^
43+
44+
error: iterating over unordered hash-based type
45+
--> $DIR/iter_over_hash_type.rs:28:5
46+
|
47+
LL | / for x in hash_map.values() {
48+
LL | | let _ = x;
49+
LL | | }
50+
| |_____^
51+
52+
error: aborting due to 6 previous errors
53+

tests/ui/unnecessary_pattern_matching.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ fn main() {
88
let _ = Some(5).map_or(false, |n| 5 == n);
99
let _ = Some(5).map_or(true, |n| n != 5);
1010
let _ = Some(5).map_or(true, |n| 5 != n);
11-
let _ = Some(vec![5]).map_or(false, |n| n == [5]);
1211

1312
// shouldnt trigger
1413
let _ = Some(5).map_or(true, |n| n == 5);
1514
let _ = Some(5).map_or(true, |n| 5 == n);
1615
let _ = Some(5).map_or(false, |n| n != 5);
1716
let _ = Some(5).map_or(false, |n| 5 != n);
1817
let _ = Some(5).map_or(false, |n| n == n);
18+
let _ = Some(vec![5]).map_or(false, |n| n == [5]);
1919
let _ = Some(vec![1]).map_or(false, |n| vec![2] == n);
2020
let _ = Some(5).map_or(false, |n| n == if 2 > 1 { n } else { 0 });
2121
}

0 commit comments

Comments
 (0)