Skip to content
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

Fix suggestion for deref expressions in redundant_pattern_matching #7949

Merged
merged 1 commit into from
Nov 9, 2021
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
14 changes: 10 additions & 4 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,8 @@ mod redundant_pattern_match {
use super::REDUNDANT_PATTERN_MATCHING;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::higher;
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::source::snippet;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, is_type_lang_item, match_type};
use clippy_utils::{is_lang_ctor, is_qpath_def_path, is_trait_method, paths};
use if_chain::if_chain;
Expand All @@ -1795,7 +1796,7 @@ mod redundant_pattern_match {
use rustc_hir::LangItem::{OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
use rustc_hir::{
intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath,
Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath, UnOp,
};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
Expand Down Expand Up @@ -2049,8 +2050,10 @@ mod redundant_pattern_match {

let result_expr = match &let_expr.kind {
ExprKind::AddrOf(_, _, borrowed) => borrowed,
ExprKind::Unary(UnOp::Deref, deref) => deref,
_ => let_expr,
};

span_lint_and_then(
cx,
REDUNDANT_PATTERN_MATCHING,
Expand All @@ -2069,12 +2072,15 @@ mod redundant_pattern_match {
// ^^^^^^^^^^^^^^^^^^^
let span = expr_span.until(op_span.shrink_to_hi());

let mut app = if needs_drop {
let app = if needs_drop {
Applicability::MaybeIncorrect
} else {
Applicability::MachineApplicable
};
let sugg = snippet_with_applicability(cx, op_span, "_", &mut app);

let sugg = Sugg::hir_with_macro_callsite(cx, result_expr, "_")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use with_macro_callsite here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, just saw the test update. Nvm.

.maybe_par()
.to_string();

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

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/redundant_pattern_matching_option.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ const fn issue6067() {

None::<()>.is_none();
}

#[allow(clippy::deref_addrof, dead_code)]
fn issue7921() {
if (&None::<()>).is_none() {}
if (&None::<()>).is_none() {}
}
6 changes: 6 additions & 0 deletions tests/ui/redundant_pattern_matching_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,9 @@ const fn issue6067() {
None => true,
};
}

#[allow(clippy::deref_addrof, dead_code)]
fn issue7921() {
if let None = *(&None::<()>) {}
if let None = *&None::<()> {}
}
14 changes: 13 additions & 1 deletion tests/ui/redundant_pattern_matching_option.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,17 @@ LL | | None => true,
LL | | };
| |_____^ help: try this: `None::<()>.is_none()`

error: aborting due to 19 previous errors
error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching_option.rs:101:12
|
LL | if let None = *(&None::<()>) {}
| -------^^^^----------------- help: try this: `if (&None::<()>).is_none()`

error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching_option.rs:102:12
|
LL | if let None = *&None::<()> {}
| -------^^^^--------------- help: try this: `if (&None::<()>).is_none()`

error: aborting due to 21 previous errors

4 changes: 2 additions & 2 deletions tests/ui/redundant_pattern_matching_result.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ fn issue5504() {
}

fn try_result_opt() -> Result<i32, i32> {
while r#try!(result_opt()).is_some() {}
if r#try!(result_opt()).is_some() {}
while (r#try!(result_opt())).is_some() {}
if (r#try!(result_opt())).is_some() {}
Ok(42)
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/redundant_pattern_matching_result.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching_result.rs:85:19
|
LL | while let Some(_) = r#try!(result_opt()) {}
| ----------^^^^^^^----------------------- help: try this: `while r#try!(result_opt()).is_some()`
| ----------^^^^^^^----------------------- help: try this: `while (r#try!(result_opt())).is_some()`

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

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