mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-25 11:57:25 +00:00
add paren before '?' when suggesting deref
This commit is contained in:
parent
05e7d5481b
commit
145ebb1cd7
4 changed files with 22 additions and 4 deletions
|
@ -4,7 +4,7 @@ use clippy_utils::source::snippet_with_context;
|
||||||
use clippy_utils::sugg;
|
use clippy_utils::sugg;
|
||||||
use clippy_utils::ty::is_copy;
|
use clippy_utils::ty::is_copy;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{BindingAnnotation, Expr, ExprKind, MatchSource, Node, PatKind};
|
use rustc_hir::{BindingAnnotation, Expr, ExprKind, MatchSource, Node, PatKind, QPath};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_middle::ty::{self, adjustment::Adjust};
|
use rustc_middle::ty::{self, adjustment::Adjust};
|
||||||
use rustc_span::symbol::{sym, Symbol};
|
use rustc_span::symbol::{sym, Symbol};
|
||||||
|
@ -86,6 +86,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: Symbol,
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
|
ExprKind::Call(hir_callee, _) => match hir_callee.kind {
|
||||||
|
ExprKind::Path(QPath::LangItem(rustc_hir::LangItem::TryTraitBranch, _, _)) => true,
|
||||||
|
_ => false,
|
||||||
|
},
|
||||||
ExprKind::MethodCall(_, [self_arg, ..], _) if expr.hir_id == self_arg.hir_id => true,
|
ExprKind::MethodCall(_, [self_arg, ..], _) if expr.hir_id == self_arg.hir_id => true,
|
||||||
ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
|
ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
|
||||||
| ExprKind::Field(..)
|
| ExprKind::Field(..)
|
||||||
|
|
|
@ -21,7 +21,7 @@ fn is_ascii(ch: char) -> bool {
|
||||||
ch.is_ascii()
|
ch.is_ascii()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clone_on_copy() {
|
fn clone_on_copy() -> Option<(i32)> {
|
||||||
42;
|
42;
|
||||||
|
|
||||||
vec![1].clone(); // ok, not a Copy type
|
vec![1].clone(); // ok, not a Copy type
|
||||||
|
@ -71,4 +71,8 @@ fn clone_on_copy() {
|
||||||
// Issue #5436
|
// Issue #5436
|
||||||
let mut vec = Vec::new();
|
let mut vec = Vec::new();
|
||||||
vec.push(42);
|
vec.push(42);
|
||||||
|
|
||||||
|
let opt: &Option<i32> = &None;
|
||||||
|
let value = (*opt)?;
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ fn is_ascii(ch: char) -> bool {
|
||||||
ch.is_ascii()
|
ch.is_ascii()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clone_on_copy() {
|
fn clone_on_copy() -> Option<(i32)> {
|
||||||
42.clone();
|
42.clone();
|
||||||
|
|
||||||
vec![1].clone(); // ok, not a Copy type
|
vec![1].clone(); // ok, not a Copy type
|
||||||
|
@ -71,4 +71,8 @@ fn clone_on_copy() {
|
||||||
// Issue #5436
|
// Issue #5436
|
||||||
let mut vec = Vec::new();
|
let mut vec = Vec::new();
|
||||||
vec.push(42.clone());
|
vec.push(42.clone());
|
||||||
|
|
||||||
|
let opt: &Option<i32> = &None;
|
||||||
|
let value = opt.clone()?;
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,5 +48,11 @@ error: using `clone` on type `i32` which implements the `Copy` trait
|
||||||
LL | vec.push(42.clone());
|
LL | vec.push(42.clone());
|
||||||
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
|
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
|
||||||
|
|
||||||
error: aborting due to 8 previous errors
|
error: using `clone` on type `std::option::Option<i32>` which implements the `Copy` trait
|
||||||
|
--> $DIR/clone_on_copy.rs:76:17
|
||||||
|
|
|
||||||
|
LL | let value = opt.clone()?;
|
||||||
|
| ^^^^^^^^^^^ help: try dereferencing it: `(*opt)`
|
||||||
|
|
||||||
|
error: aborting due to 9 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue