From 2d4c45ff62cc97bb234dde32dbc3a80f2c33811d Mon Sep 17 00:00:00 2001 From: robert-mccausland Date: Mon, 17 Nov 2025 19:50:35 +0000 Subject: [PATCH 1/2] feat: change desuragered match suggestions to NonParen --- clippy_utils/src/sugg.rs | 11 +++++-- .../redundant_pattern_matching_option.fixed | 29 +++++++++++++++++++ tests/ui/redundant_pattern_matching_option.rs | 29 +++++++++++++++++++ .../redundant_pattern_matching_option.stderr | 14 ++++++++- 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/clippy_utils/src/sugg.rs b/clippy_utils/src/sugg.rs index 2593df103527..2ef2afb45071 100644 --- a/clippy_utils/src/sugg.rs +++ b/clippy_utils/src/sugg.rs @@ -8,7 +8,7 @@ use rustc_ast::util::parser::AssocOp; use rustc_ast::{UnOp, ast}; use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; -use rustc_hir::{self as hir, Closure, ExprKind, HirId, MutTy, Node, TyKind}; +use rustc_hir::{self as hir, Closure, ExprKind, HirId, MatchSource, MutTy, Node, TyKind}; use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId}; use rustc_lint::{EarlyContext, LateContext, LintContext}; use rustc_middle::hir::place::ProjectionKind; @@ -146,7 +146,9 @@ impl<'a> Sugg<'a> { | ExprKind::Let(..) | ExprKind::Closure { .. } | ExprKind::Unary(..) - | ExprKind::Match(..) => Sugg::MaybeParen(get_snippet(expr.span)), + | ExprKind::Match(_, _, + MatchSource::Normal | MatchSource::Postfix | MatchSource::ForLoopDesugar + ) => Sugg::MaybeParen(get_snippet(expr.span)), ExprKind::Continue(..) | ExprKind::Yield(..) | ExprKind::Array(..) @@ -169,7 +171,10 @@ impl<'a> Sugg<'a> { | ExprKind::Tup(..) | ExprKind::Use(..) | ExprKind::Err(_) - | ExprKind::UnsafeBinderCast(..) => Sugg::NonParen(get_snippet(expr.span)), + | ExprKind::UnsafeBinderCast(..) + | ExprKind::Match(_, _, + MatchSource::AwaitDesugar | MatchSource::TryDesugar(_) | MatchSource::FormatArgs + ) => Sugg::NonParen(get_snippet(expr.span)), ExprKind::DropTemps(inner) => Self::hir_from_snippet(cx, inner, get_snippet), ExprKind::Assign(lhs, rhs, _) => { Sugg::BinOp(AssocOp::Assign, get_snippet(lhs.span), get_snippet(rhs.span)) diff --git a/tests/ui/redundant_pattern_matching_option.fixed b/tests/ui/redundant_pattern_matching_option.fixed index a54a4ce13d53..08903ef7fdda 100644 --- a/tests/ui/redundant_pattern_matching_option.fixed +++ b/tests/ui/redundant_pattern_matching_option.fixed @@ -166,3 +166,32 @@ fn issue13902() { //~^ redundant_pattern_matching } } + +fn issue16045() { + fn f() -> Result<(), ()> { + let x = Ok::<_, ()>(Some(123)); + if x?.is_some() { + //~^ redundant_pattern_matching + } + + Ok(()) + } + + async fn g() { + struct F { + x: Option, + } + + impl Future for F { + type Output = Option; + + fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll { + std::task::Poll::Ready(self.x) + } + } + let x = F { x: Some(123) }; + if x.await.is_some() { + //~^ redundant_pattern_matching + } + } +} diff --git a/tests/ui/redundant_pattern_matching_option.rs b/tests/ui/redundant_pattern_matching_option.rs index 7252fce8cce6..95eff3f9ebf9 100644 --- a/tests/ui/redundant_pattern_matching_option.rs +++ b/tests/ui/redundant_pattern_matching_option.rs @@ -202,3 +202,32 @@ fn issue13902() { //~^ redundant_pattern_matching } } + +fn issue16045() { + fn f() -> Result<(), ()> { + let x = Ok::<_, ()>(Some(123)); + if let Some(_) = x? { + //~^ redundant_pattern_matching + } + + Ok(()) + } + + async fn g() { + struct F { + x: Option, + } + + impl Future for F { + type Output = Option; + + fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll { + std::task::Poll::Ready(self.x) + } + } + let x = F { x: Some(123) }; + if let Some(_) = x.await { + //~^ redundant_pattern_matching + } + } +} diff --git a/tests/ui/redundant_pattern_matching_option.stderr b/tests/ui/redundant_pattern_matching_option.stderr index e5a6598898aa..6fd0c5a6f859 100644 --- a/tests/ui/redundant_pattern_matching_option.stderr +++ b/tests/ui/redundant_pattern_matching_option.stderr @@ -224,5 +224,17 @@ error: redundant pattern matching, consider using `is_none()` LL | let _ = matches!(*p, None); | ^^^^^^^^^^^^^^^^^^ help: try: `(*p).is_none()` -error: aborting due to 31 previous errors +error: redundant pattern matching, consider using `is_some()` + --> tests/ui/redundant_pattern_matching_option.rs:209:16 + | +LL | if let Some(_) = x? { + | -------^^^^^^^----- help: try: `if x?.is_some()` + +error: redundant pattern matching, consider using `is_some()` + --> tests/ui/redundant_pattern_matching_option.rs:229:16 + | +LL | if let Some(_) = x.await { + | -------^^^^^^^---------- help: try: `if x.await.is_some()` + +error: aborting due to 33 previous errors From 466646f54f3f3275c9c07421c8bc2b9ee608c661 Mon Sep 17 00:00:00 2001 From: robert-mccausland Date: Mon, 17 Nov 2025 20:07:55 +0000 Subject: [PATCH 2/2] test: add test case to cast_possible_wrap --- tests/ui/cast.rs | 10 ++++++++++ tests/ui/cast.stderr | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/ui/cast.rs b/tests/ui/cast.rs index fab02bf7b24e..ff2791c45730 100644 --- a/tests/ui/cast.rs +++ b/tests/ui/cast.rs @@ -582,3 +582,13 @@ mod issue14150 { //~^ cast_possible_wrap } } + +fn issue16045() { + fn f() -> Result<(), ()> { + let val = Ok::<_, ()>(0u8); + _ = val? as i8; + //~^ cast_possible_wrap + + Ok(()) + } +} diff --git a/tests/ui/cast.stderr b/tests/ui/cast.stderr index 8c48855123f9..0ff1dc11c3ac 100644 --- a/tests/ui/cast.stderr +++ b/tests/ui/cast.stderr @@ -764,5 +764,11 @@ error: casting `u8` to `i8` may wrap around the value LL | _ = 1u8 as i8; | ^^^^^^^^^ -error: aborting due to 94 previous errors +error: casting `u8` to `i8` may wrap around the value + --> tests/ui/cast.rs:589:13 + | +LL | _ = val? as i8; + | ^^^^^^^^^^ help: if this is intentional, use `cast_signed()` instead: `val?.cast_signed()` + +error: aborting due to 95 previous errors