Skip to content

Commit c94d62b

Browse files
committed
Auto merge of rust-lang#7949 - Serial-ATA:issue-7921, r=flip1995
Fix suggestion for deref expressions in redundant_pattern_matching changelog: Fix suggestion for deref expressions in [`redundant_pattern_matching`] closes: rust-lang#7921
2 parents 830f220 + e54c341 commit c94d62b

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
@@ -1782,7 +1782,8 @@ mod redundant_pattern_match {
17821782
use super::REDUNDANT_PATTERN_MATCHING;
17831783
use clippy_utils::diagnostics::span_lint_and_then;
17841784
use clippy_utils::higher;
1785-
use clippy_utils::source::{snippet, snippet_with_applicability};
1785+
use clippy_utils::source::snippet;
1786+
use clippy_utils::sugg::Sugg;
17861787
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, is_type_lang_item, match_type};
17871788
use clippy_utils::{is_lang_ctor, is_qpath_def_path, is_trait_method, paths};
17881789
use if_chain::if_chain;
@@ -1792,7 +1793,7 @@ mod redundant_pattern_match {
17921793
use rustc_hir::LangItem::{OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
17931794
use rustc_hir::{
17941795
intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
1795-
Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath,
1796+
Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath, UnOp,
17961797
};
17971798
use rustc_lint::LateContext;
17981799
use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
@@ -2046,8 +2047,10 @@ mod redundant_pattern_match {
20462047

20472048
let result_expr = match &let_expr.kind {
20482049
ExprKind::AddrOf(_, _, borrowed) => borrowed,
2050+
ExprKind::Unary(UnOp::Deref, deref) => deref,
20492051
_ => let_expr,
20502052
};
2053+
20512054
span_lint_and_then(
20522055
cx,
20532056
REDUNDANT_PATTERN_MATCHING,
@@ -2066,12 +2069,15 @@ mod redundant_pattern_match {
20662069
// ^^^^^^^^^^^^^^^^^^^
20672070
let span = expr_span.until(op_span.shrink_to_hi());
20682071

2069-
let mut app = if needs_drop {
2072+
let app = if needs_drop {
20702073
Applicability::MaybeIncorrect
20712074
} else {
20722075
Applicability::MachineApplicable
20732076
};
2074-
let sugg = snippet_with_applicability(cx, op_span, "_", &mut app);
2077+
2078+
let sugg = Sugg::hir_with_macro_callsite(cx, result_expr, "_")
2079+
.maybe_par()
2080+
.to_string();
20752081

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

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)