Skip to content

Commit 145ebb1

Browse files
committed
add paren before '?' when suggesting deref
1 parent 05e7d54 commit 145ebb1

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

clippy_lints/src/methods/clone_on_copy.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::source::snippet_with_context;
44
use clippy_utils::sugg;
55
use clippy_utils::ty::is_copy;
66
use rustc_errors::Applicability;
7-
use rustc_hir::{BindingAnnotation, Expr, ExprKind, MatchSource, Node, PatKind};
7+
use rustc_hir::{BindingAnnotation, Expr, ExprKind, MatchSource, Node, PatKind, QPath};
88
use rustc_lint::LateContext;
99
use rustc_middle::ty::{self, adjustment::Adjust};
1010
use rustc_span::symbol::{sym, Symbol};
@@ -86,6 +86,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: Symbol,
8686
{
8787
return;
8888
},
89+
ExprKind::Call(hir_callee, _) => match hir_callee.kind {
90+
ExprKind::Path(QPath::LangItem(rustc_hir::LangItem::TryTraitBranch, _, _)) => true,
91+
_ => false,
92+
},
8993
ExprKind::MethodCall(_, [self_arg, ..], _) if expr.hir_id == self_arg.hir_id => true,
9094
ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
9195
| ExprKind::Field(..)

tests/ui/clone_on_copy.fixed

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn is_ascii(ch: char) -> bool {
2121
ch.is_ascii()
2222
}
2323

24-
fn clone_on_copy() {
24+
fn clone_on_copy() -> Option<(i32)> {
2525
42;
2626

2727
vec![1].clone(); // ok, not a Copy type
@@ -71,4 +71,8 @@ fn clone_on_copy() {
7171
// Issue #5436
7272
let mut vec = Vec::new();
7373
vec.push(42);
74+
75+
let opt: &Option<i32> = &None;
76+
let value = (*opt)?;
77+
None
7478
}

tests/ui/clone_on_copy.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn is_ascii(ch: char) -> bool {
2121
ch.is_ascii()
2222
}
2323

24-
fn clone_on_copy() {
24+
fn clone_on_copy() -> Option<(i32)> {
2525
42.clone();
2626

2727
vec![1].clone(); // ok, not a Copy type
@@ -71,4 +71,8 @@ fn clone_on_copy() {
7171
// Issue #5436
7272
let mut vec = Vec::new();
7373
vec.push(42.clone());
74+
75+
let opt: &Option<i32> = &None;
76+
let value = opt.clone()?;
77+
None
7478
}

tests/ui/clone_on_copy.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@ error: using `clone` on type `i32` which implements the `Copy` trait
4848
LL | vec.push(42.clone());
4949
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
5050

51-
error: aborting due to 8 previous errors
51+
error: using `clone` on type `std::option::Option<i32>` which implements the `Copy` trait
52+
--> $DIR/clone_on_copy.rs:76:17
53+
|
54+
LL | let value = opt.clone()?;
55+
| ^^^^^^^^^^^ help: try dereferencing it: `(*opt)`
56+
57+
error: aborting due to 9 previous errors
5258

0 commit comments

Comments
 (0)