Skip to content

Commit 5dc90a1

Browse files
committed
Auto merge of rust-lang#9282 - macovedj:clone-on-copy-try-precedence, r=flip1995
add paren before '?' when suggesting deref for clone_on_copy changelog: none fixes rust-lang#9277
2 parents 05e7d54 + 503c03c commit 5dc90a1

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

clippy_lints/src/methods/clone_on_copy.rs

+6-1
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,11 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: Symbol,
8686
{
8787
return;
8888
},
89+
// ? is a Call, makes sure not to rec *x?, but rather (*x)?
90+
ExprKind::Call(hir_callee, _) => matches!(
91+
hir_callee.kind,
92+
ExprKind::Path(QPath::LangItem(rustc_hir::LangItem::TryTraitBranch, _, _))
93+
),
8994
ExprKind::MethodCall(_, [self_arg, ..], _) if expr.hir_id == self_arg.hir_id => true,
9095
ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
9196
| ExprKind::Field(..)

tests/ui/clone_on_copy.fixed

+6-1
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,9 @@ fn clone_on_copy() {
7171
// Issue #5436
7272
let mut vec = Vec::new();
7373
vec.push(42);
74+
75+
// Issue #9277
76+
let opt: &Option<i32> = &None;
77+
let value = (*opt)?; // operator precedence needed (*opt)?
78+
None
7479
}

tests/ui/clone_on_copy.rs

+6-1
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,9 @@ fn clone_on_copy() {
7171
// Issue #5436
7272
let mut vec = Vec::new();
7373
vec.push(42.clone());
74+
75+
// Issue #9277
76+
let opt: &Option<i32> = &None;
77+
let value = opt.clone()?; // operator precedence needed (*opt)?
78+
None
7479
}

tests/ui/clone_on_copy.stderr

+7-1
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:77:17
53+
|
54+
LL | let value = opt.clone()?; // operator precedence needed (*opt)?
55+
| ^^^^^^^^^^^ help: try dereferencing it: `(*opt)`
56+
57+
error: aborting due to 9 previous errors
5258

0 commit comments

Comments
 (0)