Skip to content
Open
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
21 changes: 19 additions & 2 deletions clippy_lints/src/matches/redundant_pattern_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use clippy_utils::res::{MaybeDef, MaybeTypeckRes};
use clippy_utils::sugg::{Sugg, make_unop};
use clippy_utils::ty::needs_ordered_drop;
use clippy_utils::visitors::{any_temporaries_need_ordered_drop, for_each_expr_without_closures};
use clippy_utils::{higher, is_expn_of, sym};
use clippy_utils::{get_parent_expr, higher, is_expn_of, sym};
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::LangItem::{self, OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Arm, Expr, ExprKind, Node, Pat, PatExpr, PatExprKind, PatKind, QPath, UnOp};
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, Node, Pat, PatExpr, PatExprKind, PatKind, QPath, UnOp};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, GenericArgKind, Ty};
use rustc_span::{Span, Symbol, kw};
Expand Down Expand Up @@ -312,6 +312,9 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
if let Some(guard) = maybe_guard {
let guard = Sugg::hir_with_context(cx, guard, ctxt, "..", &mut app);
let _ = write!(sugg, " && {}", guard.maybe_paren());
if guard_sugg_needs_parens(cx, expr) {
sugg = format!("({sugg})");
}
}

diag.span_suggestion_verbose(expr_span, format!("consider using `{good_method}`"), sugg, app);
Expand All @@ -320,6 +323,20 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
}
}

/// Whether the appended `&& guard` makes the suggestion bind looser than `expr`'s
/// parent, so the whole `recv.method() && guard` must be wrapped in parentheses.
fn guard_sugg_needs_parens(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let Some(parent) = get_parent_expr(cx, expr) else {
return false;
};
match parent.kind {
ExprKind::Binary(op, ..) => !matches!(op.node, BinOpKind::And | BinOpKind::Or),
ExprKind::Unary(..) | ExprKind::AddrOf(..) | ExprKind::Cast(..) => true,
ExprKind::MethodCall(_, receiver, ..) => receiver.hir_id == expr.hir_id,
_ => false,
}
}

fn found_good_method<'tcx>(
cx: &LateContext<'_>,
arms: &'tcx [Arm<'tcx>; 2],
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/redundant_pattern_matching_option.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ fn issue_11174_edge_cases<T>(boolean: bool, boolean2: bool, maybe_some: Option<T
};
}

fn issue_17286(a: bool, b: bool, opt: Option<i32>) {
let _ = !(opt.is_none() && a);
//~^ redundant_pattern_matching
let _ = &(opt.is_none() && a);
//~^ redundant_pattern_matching
let _ = (opt.is_none() && a) as u8;
//~^ redundant_pattern_matching
let _ = (opt.is_none() && a) == b;
//~^ redundant_pattern_matching
let _ = (opt.is_none() && a).then_some(1);
//~^ redundant_pattern_matching
let _ = opt.is_none() && a;
//~^ redundant_pattern_matching
if opt.is_none() && a {}
//~^ redundant_pattern_matching
let _ = opt.is_none() && a && b;
//~^ redundant_pattern_matching
let _ = opt.is_none() && a || b;
//~^ redundant_pattern_matching
let _ = b.then_some(opt.is_none() && a);
//~^ redundant_pattern_matching
}

fn main() {
if None::<()>.is_none() {}
//~^ redundant_pattern_matching
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/redundant_pattern_matching_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ fn issue_11174_edge_cases<T>(boolean: bool, boolean2: bool, maybe_some: Option<T
};
}

fn issue_17286(a: bool, b: bool, opt: Option<i32>) {
let _ = !matches!(opt, None if a);
//~^ redundant_pattern_matching
let _ = &matches!(opt, None if a);
//~^ redundant_pattern_matching
let _ = matches!(opt, None if a) as u8;
//~^ redundant_pattern_matching
let _ = matches!(opt, None if a) == b;
//~^ redundant_pattern_matching
let _ = matches!(opt, None if a).then_some(1);
//~^ redundant_pattern_matching
let _ = matches!(opt, None if a);
//~^ redundant_pattern_matching
if matches!(opt, None if a) {}
//~^ redundant_pattern_matching
let _ = matches!(opt, None if a) && b;
//~^ redundant_pattern_matching
let _ = matches!(opt, None if a) || b;
//~^ redundant_pattern_matching
let _ = b.then_some(matches!(opt, None if a));
//~^ redundant_pattern_matching
}

fn main() {
if let None = None::<()> {}
//~^ redundant_pattern_matching
Expand Down
Loading
Loading