feat: change desuragered match suggestions to NonParen

This commit is contained in:
robert-mccausland 2025-11-17 19:50:35 +00:00
parent 03ab7b832a
commit 2d4c45ff62
4 changed files with 79 additions and 4 deletions

View file

@ -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))

View file

@ -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<u32>,
}
impl Future for F {
type Output = Option<u32>;
fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
std::task::Poll::Ready(self.x)
}
}
let x = F { x: Some(123) };
if x.await.is_some() {
//~^ redundant_pattern_matching
}
}
}

View file

@ -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<u32>,
}
impl Future for F {
type Output = Option<u32>;
fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
std::task::Poll::Ready(self.x)
}
}
let x = F { x: Some(123) };
if let Some(_) = x.await {
//~^ redundant_pattern_matching
}
}
}

View file

@ -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