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
11 changes: 8 additions & 3 deletions clippy_utils/src/sugg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(..)
Expand All @@ -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))
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
8 changes: 7 additions & 1 deletion tests/ui/cast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

29 changes: 29 additions & 0 deletions tests/ui/redundant_pattern_matching_option.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
29 changes: 29 additions & 0 deletions tests/ui/redundant_pattern_matching_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
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 @@ -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

Loading