Skip to content

Commit 1568c64

Browse files
committed
fix: respect crate's edition
1 parent 6042a83 commit 1568c64

File tree

7 files changed

+584
-120
lines changed

7 files changed

+584
-120
lines changed

clippy_lints/src/methods/should_implement_trait.rs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_hir as hir;
55
use rustc_hir::{FnSig, ImplItem};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty::Ty;
8+
use rustc_span::edition::Edition::{self, Edition2015, Edition2021};
89
use rustc_span::{Symbol, kw};
910

1011
use super::SHOULD_IMPLEMENT_TRAIT;
@@ -31,6 +32,7 @@ pub(super) fn check_impl_item<'tcx>(
3132
.is_none_or(|first_arg_ty| method_config.self_kind.matches(cx, self_ty, first_arg_ty))
3233
&& fn_header_equals(method_config.fn_header, sig.header)
3334
&& method_config.lifetime_param_cond(impl_item)
35+
&& method_config.in_prelude_since <= cx.tcx.sess.edition()
3436
{
3537
span_lint_and_help(
3638
cx,
@@ -69,6 +71,7 @@ struct ShouldImplTraitCase {
6971
output_type: OutType,
7072
// certain methods with explicit lifetimes can't implement the equivalent trait method
7173
lint_explicit_lifetime: bool,
74+
in_prelude_since: Edition,
7275
}
7376
impl ShouldImplTraitCase {
7477
const fn new(
@@ -79,6 +82,7 @@ impl ShouldImplTraitCase {
7982
self_kind: SelfKind,
8083
output_type: OutType,
8184
lint_explicit_lifetime: bool,
85+
in_prelude_since: Edition,
8286
) -> ShouldImplTraitCase {
8387
ShouldImplTraitCase {
8488
trait_name,
@@ -88,6 +92,7 @@ impl ShouldImplTraitCase {
8892
self_kind,
8993
output_type,
9094
lint_explicit_lifetime,
95+
in_prelude_since,
9196
}
9297
}
9398

@@ -106,36 +111,36 @@ impl ShouldImplTraitCase {
106111

107112
#[rustfmt::skip]
108113
const TRAIT_METHODS: [ShouldImplTraitCase; 30] = [
109-
ShouldImplTraitCase::new("std::ops::Add", sym::add, 2, FN_HEADER, SelfKind::Value, OutType::Any, true ),
110-
ShouldImplTraitCase::new("std::convert::AsMut", sym::as_mut, 1, FN_HEADER, SelfKind::RefMut, OutType::Ref, true ),
111-
ShouldImplTraitCase::new("std::convert::AsRef", sym::as_ref, 1, FN_HEADER, SelfKind::Ref, OutType::Ref, true ),
112-
ShouldImplTraitCase::new("std::ops::BitAnd", sym::bitand, 2, FN_HEADER, SelfKind::Value, OutType::Any, true ),
113-
ShouldImplTraitCase::new("std::ops::BitOr", sym::bitor, 2, FN_HEADER, SelfKind::Value, OutType::Any, true ),
114-
ShouldImplTraitCase::new("std::ops::BitXor", sym::bitxor, 2, FN_HEADER, SelfKind::Value, OutType::Any, true ),
115-
ShouldImplTraitCase::new("std::borrow::Borrow", sym::borrow, 1, FN_HEADER, SelfKind::Ref, OutType::Ref, true ),
116-
ShouldImplTraitCase::new("std::borrow::BorrowMut", sym::borrow_mut, 1, FN_HEADER, SelfKind::RefMut, OutType::Ref, true ),
117-
ShouldImplTraitCase::new("std::clone::Clone", sym::clone, 1, FN_HEADER, SelfKind::Ref, OutType::Any, true ),
118-
ShouldImplTraitCase::new("std::cmp::Ord", sym::cmp, 2, FN_HEADER, SelfKind::Ref, OutType::Any, true ),
119-
ShouldImplTraitCase::new("std::default::Default", kw::Default, 0, FN_HEADER, SelfKind::No, OutType::Any, true ),
120-
ShouldImplTraitCase::new("std::ops::Deref", sym::deref, 1, FN_HEADER, SelfKind::Ref, OutType::Ref, true ),
121-
ShouldImplTraitCase::new("std::ops::DerefMut", sym::deref_mut, 1, FN_HEADER, SelfKind::RefMut, OutType::Ref, true ),
122-
ShouldImplTraitCase::new("std::ops::Div", sym::div, 2, FN_HEADER, SelfKind::Value, OutType::Any, true ),
123-
ShouldImplTraitCase::new("std::ops::Drop", sym::drop, 1, FN_HEADER, SelfKind::RefMut, OutType::Unit, true ),
124-
ShouldImplTraitCase::new("std::cmp::PartialEq", sym::eq, 2, FN_HEADER, SelfKind::Ref, OutType::Bool, true ),
125-
ShouldImplTraitCase::new("std::iter::FromIterator", sym::from_iter, 1, FN_HEADER, SelfKind::No, OutType::Any, true ),
126-
ShouldImplTraitCase::new("std::str::FromStr", sym::from_str, 1, FN_HEADER, SelfKind::No, OutType::Any, true ),
127-
ShouldImplTraitCase::new("std::hash::Hash", sym::hash, 2, FN_HEADER, SelfKind::Ref, OutType::Unit, true ),
128-
ShouldImplTraitCase::new("std::ops::Index", sym::index, 2, FN_HEADER, SelfKind::Ref, OutType::Ref, true ),
129-
ShouldImplTraitCase::new("std::ops::IndexMut", sym::index_mut, 2, FN_HEADER, SelfKind::RefMut, OutType::Ref, true ),
130-
ShouldImplTraitCase::new("std::iter::IntoIterator", sym::into_iter, 1, FN_HEADER, SelfKind::Value, OutType::Any, true ),
131-
ShouldImplTraitCase::new("std::ops::Mul", sym::mul, 2, FN_HEADER, SelfKind::Value, OutType::Any, true ),
132-
ShouldImplTraitCase::new("std::ops::Neg", sym::neg, 1, FN_HEADER, SelfKind::Value, OutType::Any, true ),
133-
ShouldImplTraitCase::new("std::iter::Iterator", sym::next, 1, FN_HEADER, SelfKind::RefMut, OutType::Any, false),
134-
ShouldImplTraitCase::new("std::ops::Not", sym::not, 1, FN_HEADER, SelfKind::Value, OutType::Any, true ),
135-
ShouldImplTraitCase::new("std::ops::Rem", sym::rem, 2, FN_HEADER, SelfKind::Value, OutType::Any, true ),
136-
ShouldImplTraitCase::new("std::ops::Shl", sym::shl, 2, FN_HEADER, SelfKind::Value, OutType::Any, true ),
137-
ShouldImplTraitCase::new("std::ops::Shr", sym::shr, 2, FN_HEADER, SelfKind::Value, OutType::Any, true ),
138-
ShouldImplTraitCase::new("std::ops::Sub", sym::sub, 2, FN_HEADER, SelfKind::Value, OutType::Any, true ),
114+
ShouldImplTraitCase::new("std::ops::Add", sym::add, 2, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
115+
ShouldImplTraitCase::new("std::convert::AsMut", sym::as_mut, 1, FN_HEADER, SelfKind::RefMut, OutType::Ref, true, Edition2015),
116+
ShouldImplTraitCase::new("std::convert::AsRef", sym::as_ref, 1, FN_HEADER, SelfKind::Ref, OutType::Ref, true, Edition2015),
117+
ShouldImplTraitCase::new("std::ops::BitAnd", sym::bitand, 2, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
118+
ShouldImplTraitCase::new("std::ops::BitOr", sym::bitor, 2, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
119+
ShouldImplTraitCase::new("std::ops::BitXor", sym::bitxor, 2, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
120+
ShouldImplTraitCase::new("std::borrow::Borrow", sym::borrow, 1, FN_HEADER, SelfKind::Ref, OutType::Ref, true, Edition2015),
121+
ShouldImplTraitCase::new("std::borrow::BorrowMut", sym::borrow_mut, 1, FN_HEADER, SelfKind::RefMut, OutType::Ref, true, Edition2015),
122+
ShouldImplTraitCase::new("std::clone::Clone", sym::clone, 1, FN_HEADER, SelfKind::Ref, OutType::Any, true, Edition2015),
123+
ShouldImplTraitCase::new("std::cmp::Ord", sym::cmp, 2, FN_HEADER, SelfKind::Ref, OutType::Any, true, Edition2015),
124+
ShouldImplTraitCase::new("std::default::Default", kw::Default, 0, FN_HEADER, SelfKind::No, OutType::Any, true, Edition2015),
125+
ShouldImplTraitCase::new("std::ops::Deref", sym::deref, 1, FN_HEADER, SelfKind::Ref, OutType::Ref, true, Edition2015),
126+
ShouldImplTraitCase::new("std::ops::DerefMut", sym::deref_mut, 1, FN_HEADER, SelfKind::RefMut, OutType::Ref, true, Edition2015),
127+
ShouldImplTraitCase::new("std::ops::Div", sym::div, 2, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
128+
ShouldImplTraitCase::new("std::ops::Drop", sym::drop, 1, FN_HEADER, SelfKind::RefMut, OutType::Unit, true, Edition2015),
129+
ShouldImplTraitCase::new("std::cmp::PartialEq", sym::eq, 2, FN_HEADER, SelfKind::Ref, OutType::Bool, true, Edition2015),
130+
ShouldImplTraitCase::new("std::iter::FromIterator", sym::from_iter, 1, FN_HEADER, SelfKind::No, OutType::Any, true, Edition2021),
131+
ShouldImplTraitCase::new("std::str::FromStr", sym::from_str, 1, FN_HEADER, SelfKind::No, OutType::Any, true, Edition2015),
132+
ShouldImplTraitCase::new("std::hash::Hash", sym::hash, 2, FN_HEADER, SelfKind::Ref, OutType::Unit, true, Edition2015),
133+
ShouldImplTraitCase::new("std::ops::Index", sym::index, 2, FN_HEADER, SelfKind::Ref, OutType::Ref, true, Edition2015),
134+
ShouldImplTraitCase::new("std::ops::IndexMut", sym::index_mut, 2, FN_HEADER, SelfKind::RefMut, OutType::Ref, true, Edition2015),
135+
ShouldImplTraitCase::new("std::iter::IntoIterator", sym::into_iter, 1, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
136+
ShouldImplTraitCase::new("std::ops::Mul", sym::mul, 2, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
137+
ShouldImplTraitCase::new("std::ops::Neg", sym::neg, 1, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
138+
ShouldImplTraitCase::new("std::iter::Iterator", sym::next, 1, FN_HEADER, SelfKind::RefMut, OutType::Any, false, Edition2015),
139+
ShouldImplTraitCase::new("std::ops::Not", sym::not, 1, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
140+
ShouldImplTraitCase::new("std::ops::Rem", sym::rem, 2, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
141+
ShouldImplTraitCase::new("std::ops::Shl", sym::shl, 2, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
142+
ShouldImplTraitCase::new("std::ops::Shr", sym::shr, 2, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
143+
ShouldImplTraitCase::new("std::ops::Sub", sym::sub, 2, FN_HEADER, SelfKind::Value, OutType::Any, true, Edition2015),
139144
];
140145

141146
#[derive(Clone, Copy)]

tests/ui/should_impl_trait/method_list_1.stderr renamed to tests/ui/should_impl_trait/method_list_1.edition2015.stderr

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error: method `add` can be confused for the standard trait method `std::ops::Add::add`
2-
--> tests/ui/should_impl_trait/method_list_1.rs:24:5
2+
--> tests/ui/should_impl_trait/method_list_1.rs:27:5
33
|
44
LL | / pub fn add(self, other: T) -> T {
5-
LL | |
6-
LL | |
7-
LL | | unimplemented!()
5+
... |
86
LL | | }
97
| |_____^
108
|
@@ -13,168 +11,140 @@ LL | | }
1311
= help: to override `-D warnings` add `#[allow(clippy::should_implement_trait)]`
1412

1513
error: method `as_mut` can be confused for the standard trait method `std::convert::AsMut::as_mut`
16-
--> tests/ui/should_impl_trait/method_list_1.rs:30:5
14+
--> tests/ui/should_impl_trait/method_list_1.rs:34:5
1715
|
1816
LL | / pub fn as_mut(&mut self) -> &mut T {
19-
LL | |
20-
LL | |
21-
LL | | unimplemented!()
17+
... |
2218
LL | | }
2319
| |_____^
2420
|
2521
= help: consider implementing the trait `std::convert::AsMut` or choosing a less ambiguous method name
2622

2723
error: method `as_ref` can be confused for the standard trait method `std::convert::AsRef::as_ref`
28-
--> tests/ui/should_impl_trait/method_list_1.rs:36:5
24+
--> tests/ui/should_impl_trait/method_list_1.rs:41:5
2925
|
3026
LL | / pub fn as_ref(&self) -> &T {
31-
LL | |
32-
LL | |
33-
LL | | unimplemented!()
27+
... |
3428
LL | | }
3529
| |_____^
3630
|
3731
= help: consider implementing the trait `std::convert::AsRef` or choosing a less ambiguous method name
3832

3933
error: method `bitand` can be confused for the standard trait method `std::ops::BitAnd::bitand`
40-
--> tests/ui/should_impl_trait/method_list_1.rs:42:5
34+
--> tests/ui/should_impl_trait/method_list_1.rs:48:5
4135
|
4236
LL | / pub fn bitand(self, rhs: T) -> T {
43-
LL | |
44-
LL | |
45-
LL | | unimplemented!()
37+
... |
4638
LL | | }
4739
| |_____^
4840
|
4941
= help: consider implementing the trait `std::ops::BitAnd` or choosing a less ambiguous method name
5042

5143
error: method `bitor` can be confused for the standard trait method `std::ops::BitOr::bitor`
52-
--> tests/ui/should_impl_trait/method_list_1.rs:48:5
44+
--> tests/ui/should_impl_trait/method_list_1.rs:55:5
5345
|
5446
LL | / pub fn bitor(self, rhs: Self) -> Self {
55-
LL | |
56-
LL | |
57-
LL | | unimplemented!()
47+
... |
5848
LL | | }
5949
| |_____^
6050
|
6151
= help: consider implementing the trait `std::ops::BitOr` or choosing a less ambiguous method name
6252

6353
error: method `bitxor` can be confused for the standard trait method `std::ops::BitXor::bitxor`
64-
--> tests/ui/should_impl_trait/method_list_1.rs:54:5
54+
--> tests/ui/should_impl_trait/method_list_1.rs:62:5
6555
|
6656
LL | / pub fn bitxor(self, rhs: Self) -> Self {
67-
LL | |
68-
LL | |
69-
LL | | unimplemented!()
57+
... |
7058
LL | | }
7159
| |_____^
7260
|
7361
= help: consider implementing the trait `std::ops::BitXor` or choosing a less ambiguous method name
7462

7563
error: method `borrow` can be confused for the standard trait method `std::borrow::Borrow::borrow`
76-
--> tests/ui/should_impl_trait/method_list_1.rs:60:5
64+
--> tests/ui/should_impl_trait/method_list_1.rs:69:5
7765
|
7866
LL | / pub fn borrow(&self) -> &str {
79-
LL | |
80-
LL | |
81-
LL | | unimplemented!()
67+
... |
8268
LL | | }
8369
| |_____^
8470
|
8571
= help: consider implementing the trait `std::borrow::Borrow` or choosing a less ambiguous method name
8672

8773
error: method `borrow_mut` can be confused for the standard trait method `std::borrow::BorrowMut::borrow_mut`
88-
--> tests/ui/should_impl_trait/method_list_1.rs:66:5
74+
--> tests/ui/should_impl_trait/method_list_1.rs:76:5
8975
|
9076
LL | / pub fn borrow_mut(&mut self) -> &mut str {
91-
LL | |
92-
LL | |
93-
LL | | unimplemented!()
77+
... |
9478
LL | | }
9579
| |_____^
9680
|
9781
= help: consider implementing the trait `std::borrow::BorrowMut` or choosing a less ambiguous method name
9882

9983
error: method `clone` can be confused for the standard trait method `std::clone::Clone::clone`
100-
--> tests/ui/should_impl_trait/method_list_1.rs:72:5
84+
--> tests/ui/should_impl_trait/method_list_1.rs:83:5
10185
|
10286
LL | / pub fn clone(&self) -> Self {
103-
LL | |
104-
LL | |
105-
LL | | unimplemented!()
87+
... |
10688
LL | | }
10789
| |_____^
10890
|
10991
= help: consider implementing the trait `std::clone::Clone` or choosing a less ambiguous method name
11092

11193
error: method `cmp` can be confused for the standard trait method `std::cmp::Ord::cmp`
112-
--> tests/ui/should_impl_trait/method_list_1.rs:78:5
94+
--> tests/ui/should_impl_trait/method_list_1.rs:90:5
11395
|
11496
LL | / pub fn cmp(&self, other: &Self) -> Self {
115-
LL | |
116-
LL | |
117-
LL | | unimplemented!()
97+
... |
11898
LL | | }
11999
| |_____^
120100
|
121101
= help: consider implementing the trait `std::cmp::Ord` or choosing a less ambiguous method name
122102

123103
error: method `default` can be confused for the standard trait method `std::default::Default::default`
124-
--> tests/ui/should_impl_trait/method_list_1.rs:84:5
104+
--> tests/ui/should_impl_trait/method_list_1.rs:97:5
125105
|
126106
LL | / pub fn default() -> Self {
127-
LL | |
128-
LL | |
129-
LL | | unimplemented!()
107+
... |
130108
LL | | }
131109
| |_____^
132110
|
133111
= help: consider implementing the trait `std::default::Default` or choosing a less ambiguous method name
134112

135113
error: method `deref` can be confused for the standard trait method `std::ops::Deref::deref`
136-
--> tests/ui/should_impl_trait/method_list_1.rs:90:5
114+
--> tests/ui/should_impl_trait/method_list_1.rs:104:5
137115
|
138116
LL | / pub fn deref(&self) -> &Self {
139-
LL | |
140-
LL | |
141-
LL | | unimplemented!()
117+
... |
142118
LL | | }
143119
| |_____^
144120
|
145121
= help: consider implementing the trait `std::ops::Deref` or choosing a less ambiguous method name
146122

147123
error: method `deref_mut` can be confused for the standard trait method `std::ops::DerefMut::deref_mut`
148-
--> tests/ui/should_impl_trait/method_list_1.rs:96:5
124+
--> tests/ui/should_impl_trait/method_list_1.rs:111:5
149125
|
150126
LL | / pub fn deref_mut(&mut self) -> &mut Self {
151-
LL | |
152-
LL | |
153-
LL | | unimplemented!()
127+
... |
154128
LL | | }
155129
| |_____^
156130
|
157131
= help: consider implementing the trait `std::ops::DerefMut` or choosing a less ambiguous method name
158132

159133
error: method `div` can be confused for the standard trait method `std::ops::Div::div`
160-
--> tests/ui/should_impl_trait/method_list_1.rs:102:5
134+
--> tests/ui/should_impl_trait/method_list_1.rs:118:5
161135
|
162136
LL | / pub fn div(self, rhs: Self) -> Self {
163-
LL | |
164-
LL | |
165-
LL | | unimplemented!()
137+
... |
166138
LL | | }
167139
| |_____^
168140
|
169141
= help: consider implementing the trait `std::ops::Div` or choosing a less ambiguous method name
170142

171143
error: method `drop` can be confused for the standard trait method `std::ops::Drop::drop`
172-
--> tests/ui/should_impl_trait/method_list_1.rs:108:5
144+
--> tests/ui/should_impl_trait/method_list_1.rs:125:5
173145
|
174146
LL | / pub fn drop(&mut self) {
175-
LL | |
176-
LL | |
177-
LL | | unimplemented!()
147+
... |
178148
LL | | }
179149
| |_____^
180150
|

0 commit comments

Comments
 (0)