Skip to content

Commit e54c341

Browse files
committed
Fix suggestion for deref expressions in redundant_pattern_matching
1 parent 07f4f7c commit e54c341

6 files changed

+39
-9
lines changed

clippy_lints/src/matches.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,8 @@ mod redundant_pattern_match {
17851785
use super::REDUNDANT_PATTERN_MATCHING;
17861786
use clippy_utils::diagnostics::span_lint_and_then;
17871787
use clippy_utils::higher;
1788-
use clippy_utils::source::{snippet, snippet_with_applicability};
1788+
use clippy_utils::source::snippet;
1789+
use clippy_utils::sugg::Sugg;
17891790
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, is_type_lang_item, match_type};
17901791
use clippy_utils::{is_lang_ctor, is_qpath_def_path, is_trait_method, paths};
17911792
use if_chain::if_chain;
@@ -1795,7 +1796,7 @@ mod redundant_pattern_match {
17951796
use rustc_hir::LangItem::{OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
17961797
use rustc_hir::{
17971798
intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
1798-
Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath,
1799+
Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath, UnOp,
17991800
};
18001801
use rustc_lint::LateContext;
18011802
use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
@@ -2049,8 +2050,10 @@ mod redundant_pattern_match {
20492050

20502051
let result_expr = match &let_expr.kind {
20512052
ExprKind::AddrOf(_, _, borrowed) => borrowed,
2053+
ExprKind::Unary(UnOp::Deref, deref) => deref,
20522054
_ => let_expr,
20532055
};
2056+
20542057
span_lint_and_then(
20552058
cx,
20562059
REDUNDANT_PATTERN_MATCHING,
@@ -2069,12 +2072,15 @@ mod redundant_pattern_match {
20692072
// ^^^^^^^^^^^^^^^^^^^
20702073
let span = expr_span.until(op_span.shrink_to_hi());
20712074

2072-
let mut app = if needs_drop {
2075+
let app = if needs_drop {
20732076
Applicability::MaybeIncorrect
20742077
} else {
20752078
Applicability::MachineApplicable
20762079
};
2077-
let sugg = snippet_with_applicability(cx, op_span, "_", &mut app);
2080+
2081+
let sugg = Sugg::hir_with_macro_callsite(cx, result_expr, "_")
2082+
.maybe_par()
2083+
.to_string();
20782084

20792085
diag.span_suggestion(span, "try this", format!("{} {}.{}", keyword, sugg, good_method), app);
20802086

tests/ui/redundant_pattern_matching_option.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,9 @@ const fn issue6067() {
8080

8181
None::<()>.is_none();
8282
}
83+
84+
#[allow(clippy::deref_addrof, dead_code)]
85+
fn issue7921() {
86+
if (&None::<()>).is_none() {}
87+
if (&None::<()>).is_none() {}
88+
}

tests/ui/redundant_pattern_matching_option.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,9 @@ const fn issue6067() {
9595
None => true,
9696
};
9797
}
98+
99+
#[allow(clippy::deref_addrof, dead_code)]
100+
fn issue7921() {
101+
if let None = *(&None::<()>) {}
102+
if let None = *&None::<()> {}
103+
}

tests/ui/redundant_pattern_matching_option.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,17 @@ LL | | None => true,
130130
LL | | };
131131
| |_____^ help: try this: `None::<()>.is_none()`
132132

133-
error: aborting due to 19 previous errors
133+
error: redundant pattern matching, consider using `is_none()`
134+
--> $DIR/redundant_pattern_matching_option.rs:101:12
135+
|
136+
LL | if let None = *(&None::<()>) {}
137+
| -------^^^^----------------- help: try this: `if (&None::<()>).is_none()`
138+
139+
error: redundant pattern matching, consider using `is_none()`
140+
--> $DIR/redundant_pattern_matching_option.rs:102:12
141+
|
142+
LL | if let None = *&None::<()> {}
143+
| -------^^^^--------------- help: try this: `if (&None::<()>).is_none()`
144+
145+
error: aborting due to 21 previous errors
134146

tests/ui/redundant_pattern_matching_result.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ fn issue5504() {
7070
}
7171

7272
fn try_result_opt() -> Result<i32, i32> {
73-
while r#try!(result_opt()).is_some() {}
74-
if r#try!(result_opt()).is_some() {}
73+
while (r#try!(result_opt())).is_some() {}
74+
if (r#try!(result_opt())).is_some() {}
7575
Ok(42)
7676
}
7777

tests/ui/redundant_pattern_matching_result.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ error: redundant pattern matching, consider using `is_some()`
8888
--> $DIR/redundant_pattern_matching_result.rs:85:19
8989
|
9090
LL | while let Some(_) = r#try!(result_opt()) {}
91-
| ----------^^^^^^^----------------------- help: try this: `while r#try!(result_opt()).is_some()`
91+
| ----------^^^^^^^----------------------- help: try this: `while (r#try!(result_opt())).is_some()`
9292

9393
error: redundant pattern matching, consider using `is_some()`
9494
--> $DIR/redundant_pattern_matching_result.rs:86:16
9595
|
9696
LL | if let Some(_) = r#try!(result_opt()) {}
97-
| -------^^^^^^^----------------------- help: try this: `if r#try!(result_opt()).is_some()`
97+
| -------^^^^^^^----------------------- help: try this: `if (r#try!(result_opt())).is_some()`
9898

9999
error: redundant pattern matching, consider using `is_some()`
100100
--> $DIR/redundant_pattern_matching_result.rs:92:12

0 commit comments

Comments
 (0)