Skip to content

Commit 4095390

Browse files
committed
fix: respect crate's edition
1 parent 0b0a2fe commit 4095390

File tree

7 files changed

+597
-46
lines changed

7 files changed

+597
-46
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: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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 {
55
LL | |
@@ -13,7 +13,7 @@ LL | | }
1313
= help: to override `-D warnings` add `#[allow(clippy::should_implement_trait)]`
1414

1515
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
16+
--> tests/ui/should_impl_trait/method_list_1.rs:33:5
1717
|
1818
LL | / pub fn as_mut(&mut self) -> &mut T {
1919
LL | |
@@ -25,7 +25,7 @@ LL | | }
2525
= help: consider implementing the trait `std::convert::AsMut` or choosing a less ambiguous method name
2626

2727
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
28+
--> tests/ui/should_impl_trait/method_list_1.rs:39:5
2929
|
3030
LL | / pub fn as_ref(&self) -> &T {
3131
LL | |
@@ -37,7 +37,7 @@ LL | | }
3737
= help: consider implementing the trait `std::convert::AsRef` or choosing a less ambiguous method name
3838

3939
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
40+
--> tests/ui/should_impl_trait/method_list_1.rs:45:5
4141
|
4242
LL | / pub fn bitand(self, rhs: T) -> T {
4343
LL | |
@@ -49,7 +49,7 @@ LL | | }
4949
= help: consider implementing the trait `std::ops::BitAnd` or choosing a less ambiguous method name
5050

5151
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
52+
--> tests/ui/should_impl_trait/method_list_1.rs:51:5
5353
|
5454
LL | / pub fn bitor(self, rhs: Self) -> Self {
5555
LL | |
@@ -61,7 +61,7 @@ LL | | }
6161
= help: consider implementing the trait `std::ops::BitOr` or choosing a less ambiguous method name
6262

6363
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
64+
--> tests/ui/should_impl_trait/method_list_1.rs:57:5
6565
|
6666
LL | / pub fn bitxor(self, rhs: Self) -> Self {
6767
LL | |
@@ -73,7 +73,7 @@ LL | | }
7373
= help: consider implementing the trait `std::ops::BitXor` or choosing a less ambiguous method name
7474

7575
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
76+
--> tests/ui/should_impl_trait/method_list_1.rs:63:5
7777
|
7878
LL | / pub fn borrow(&self) -> &str {
7979
LL | |
@@ -85,7 +85,7 @@ LL | | }
8585
= help: consider implementing the trait `std::borrow::Borrow` or choosing a less ambiguous method name
8686

8787
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
88+
--> tests/ui/should_impl_trait/method_list_1.rs:69:5
8989
|
9090
LL | / pub fn borrow_mut(&mut self) -> &mut str {
9191
LL | |
@@ -97,7 +97,7 @@ LL | | }
9797
= help: consider implementing the trait `std::borrow::BorrowMut` or choosing a less ambiguous method name
9898

9999
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
100+
--> tests/ui/should_impl_trait/method_list_1.rs:75:5
101101
|
102102
LL | / pub fn clone(&self) -> Self {
103103
LL | |
@@ -109,7 +109,7 @@ LL | | }
109109
= help: consider implementing the trait `std::clone::Clone` or choosing a less ambiguous method name
110110

111111
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
112+
--> tests/ui/should_impl_trait/method_list_1.rs:81:5
113113
|
114114
LL | / pub fn cmp(&self, other: &Self) -> Self {
115115
LL | |
@@ -121,7 +121,7 @@ LL | | }
121121
= help: consider implementing the trait `std::cmp::Ord` or choosing a less ambiguous method name
122122

123123
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
124+
--> tests/ui/should_impl_trait/method_list_1.rs:87:5
125125
|
126126
LL | / pub fn default() -> Self {
127127
LL | |
@@ -133,7 +133,7 @@ LL | | }
133133
= help: consider implementing the trait `std::default::Default` or choosing a less ambiguous method name
134134

135135
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
136+
--> tests/ui/should_impl_trait/method_list_1.rs:93:5
137137
|
138138
LL | / pub fn deref(&self) -> &Self {
139139
LL | |
@@ -145,7 +145,7 @@ LL | | }
145145
= help: consider implementing the trait `std::ops::Deref` or choosing a less ambiguous method name
146146

147147
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
148+
--> tests/ui/should_impl_trait/method_list_1.rs:99:5
149149
|
150150
LL | / pub fn deref_mut(&mut self) -> &mut Self {
151151
LL | |
@@ -157,7 +157,7 @@ LL | | }
157157
= help: consider implementing the trait `std::ops::DerefMut` or choosing a less ambiguous method name
158158

159159
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
160+
--> tests/ui/should_impl_trait/method_list_1.rs:105:5
161161
|
162162
LL | / pub fn div(self, rhs: Self) -> Self {
163163
LL | |
@@ -169,7 +169,7 @@ LL | | }
169169
= help: consider implementing the trait `std::ops::Div` or choosing a less ambiguous method name
170170

171171
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
172+
--> tests/ui/should_impl_trait/method_list_1.rs:111:5
173173
|
174174
LL | / pub fn drop(&mut self) {
175175
LL | |

0 commit comments

Comments
 (0)