@@ -23,11 +23,12 @@ pub struct DisallowedPath<const REPLACEMENT_ALLOWED: bool = true> {
23
23
path : String ,
24
24
reason : Option < String > ,
25
25
replacement : Option < String > ,
26
- /// Setting `allow_invalid` to true suppresses a warning if `path` is invalid.
26
+ /// Setting `may_not_refer_to_existing_definition` to true suppresses a warning if `path` does
27
+ /// not refer to an existing definition.
27
28
///
28
29
/// This could be useful when conditional compilation is used, or when a clippy.toml file is
29
30
/// shared among multiple projects.
30
- allow_invalid : bool ,
31
+ may_not_refer_to_existing_definition : bool ,
31
32
/// The span of the `DisallowedPath`.
32
33
///
33
34
/// Used for diagnostics.
@@ -48,7 +49,7 @@ impl<'de, const REPLACEMENT_ALLOWED: bool> Deserialize<'de> for DisallowedPath<R
48
49
path : enum_. path ( ) . to_owned ( ) ,
49
50
reason : enum_. reason ( ) . map ( ToOwned :: to_owned) ,
50
51
replacement : enum_. replacement ( ) . map ( ToOwned :: to_owned) ,
51
- allow_invalid : enum_. allow_invalid ( ) ,
52
+ may_not_refer_to_existing_definition : enum_. may_not_refer_to_existing_definition ( ) ,
52
53
span : Span :: default ( ) ,
53
54
} )
54
55
}
@@ -64,8 +65,8 @@ enum DisallowedPathEnum {
64
65
path : String ,
65
66
reason : Option < String > ,
66
67
replacement : Option < String > ,
67
- #[ serde( rename = "allow-invalid " ) ]
68
- allow_invalid : Option < bool > ,
68
+ #[ serde( rename = "may-not-refer-to-existing-definition " ) ]
69
+ may_not_refer_to_existing_definition : Option < bool > ,
69
70
} ,
70
71
}
71
72
@@ -74,7 +75,7 @@ impl<const REPLACEMENT_ALLOWED: bool> DisallowedPath<REPLACEMENT_ALLOWED> {
74
75
& self . path
75
76
}
76
77
77
- pub fn diag_amendment ( & self , span : Span ) -> impl FnOnce ( & mut Diag < ' _ , ( ) > ) + use < ' _ , REPLACEMENT_ALLOWED > {
78
+ pub fn diag_amendment ( & self , span : Span ) -> impl FnOnce ( & mut Diag < ' _ , ( ) > ) {
78
79
move |diag| {
79
80
if let Some ( replacement) = & self . replacement {
80
81
diag. span_suggestion (
@@ -119,9 +120,12 @@ impl DisallowedPathEnum {
119
120
}
120
121
}
121
122
122
- fn allow_invalid ( & self ) -> bool {
123
+ fn may_not_refer_to_existing_definition ( & self ) -> bool {
123
124
match & self {
124
- Self :: WithReason { allow_invalid, .. } => allow_invalid. unwrap_or_default ( ) ,
125
+ Self :: WithReason {
126
+ may_not_refer_to_existing_definition,
127
+ ..
128
+ } => may_not_refer_to_existing_definition. unwrap_or_default ( ) ,
125
129
Self :: Simple ( _) => false ,
126
130
}
127
131
}
@@ -133,6 +137,7 @@ pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>(
133
137
tcx : TyCtxt < ' _ > ,
134
138
disallowed_paths : & ' static [ DisallowedPath < REPLACEMENT_ALLOWED > ] ,
135
139
def_kind_predicate : impl Fn ( DefKind ) -> bool ,
140
+ predicate_description : & str ,
136
141
allow_prim_tys : bool ,
137
142
) -> (
138
143
DefIdMap < ( & ' static str , & ' static DisallowedPath < REPLACEMENT_ALLOWED > ) > ,
@@ -143,13 +148,13 @@ pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>(
143
148
FxHashMap :: default ( ) ;
144
149
for disallowed_path in disallowed_paths {
145
150
let path = disallowed_path. path ( ) ;
146
- let mut reses = clippy_utils:: def_path_res ( tcx, & path. split ( "::" ) . collect :: < Vec < _ > > ( ) ) ;
151
+ let mut resolutions = clippy_utils:: def_path_res ( tcx, & path. split ( "::" ) . collect :: < Vec < _ > > ( ) ) ;
147
152
148
- let mut found_def_id = false ;
153
+ let mut found_def_id = None ;
149
154
let mut found_prim_ty = false ;
150
- reses . retain ( |res| match res {
151
- Res :: Def ( def_kind, _ ) => {
152
- found_def_id = true ;
155
+ resolutions . retain ( |res| match res {
156
+ Res :: Def ( def_kind, def_id ) => {
157
+ found_def_id = Some ( * def_id ) ;
153
158
def_kind_predicate ( * def_kind)
154
159
} ,
155
160
Res :: PrimTy ( _) => {
@@ -159,23 +164,32 @@ pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>(
159
164
_ => false ,
160
165
} ) ;
161
166
162
- if reses . is_empty ( ) {
167
+ if resolutions . is_empty ( ) {
163
168
let span = disallowed_path. span ( ) ;
164
169
165
- if found_def_id {
166
- tcx. sess
167
- . dcx ( )
168
- . span_warn ( span, format ! ( "`{path}` is not of an appropriate kind" ) ) ;
170
+ if let Some ( def_id) = found_def_id {
171
+ tcx. sess . dcx ( ) . span_warn (
172
+ span,
173
+ format ! (
174
+ "expected a {predicate_description}, found {} {}" ,
175
+ tcx. def_descr_article( def_id) ,
176
+ tcx. def_descr( def_id)
177
+ ) ,
178
+ ) ;
169
179
} else if found_prim_ty {
170
- tcx. sess
171
- . dcx ( )
172
- . span_warn ( span, format ! ( "primitive types are not allowed: {path}" ) ) ;
173
- } else if !disallowed_path. allow_invalid {
174
- tcx. sess . dcx ( ) . span_warn ( span, format ! ( "`{path}` is invalid" ) ) ;
180
+ tcx. sess . dcx ( ) . span_warn (
181
+ span,
182
+ format ! ( "expected a {predicate_description}, found a primitive type" , ) ,
183
+ ) ;
184
+ } else if !disallowed_path. may_not_refer_to_existing_definition {
185
+ tcx. sess . dcx ( ) . span_warn (
186
+ span,
187
+ format ! ( "`{path}` does not refer to an existing {predicate_description}" ) ,
188
+ ) ;
175
189
}
176
190
}
177
191
178
- for res in reses {
192
+ for res in resolutions {
179
193
match res {
180
194
Res :: Def ( _, def_id) => {
181
195
def_ids. insert ( def_id, ( path, disallowed_path) ) ;
0 commit comments