@@ -23,11 +23,12 @@ pub struct DisallowedPath<const REPLACEMENT_ALLOWED: bool = true> {
2323 path : String ,
2424 reason : Option < String > ,
2525 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.
2728 ///
2829 /// This could be useful when conditional compilation is used, or when a clippy.toml file is
2930 /// shared among multiple projects.
30- allow_invalid : bool ,
31+ may_not_refer_to_existing_definition : bool ,
3132 /// The span of the `DisallowedPath`.
3233 ///
3334 /// Used for diagnostics.
@@ -48,7 +49,7 @@ impl<'de, const REPLACEMENT_ALLOWED: bool> Deserialize<'de> for DisallowedPath<R
4849 path : enum_. path ( ) . to_owned ( ) ,
4950 reason : enum_. reason ( ) . map ( ToOwned :: to_owned) ,
5051 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 ( ) ,
5253 span : Span :: default ( ) ,
5354 } )
5455 }
@@ -64,8 +65,8 @@ enum DisallowedPathEnum {
6465 path : String ,
6566 reason : Option < String > ,
6667 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 > ,
6970 } ,
7071}
7172
@@ -74,7 +75,7 @@ impl<const REPLACEMENT_ALLOWED: bool> DisallowedPath<REPLACEMENT_ALLOWED> {
7475 & self . path
7576 }
7677
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 < ' _ , ( ) > ) {
7879 move |diag| {
7980 if let Some ( replacement) = & self . replacement {
8081 diag. span_suggestion (
@@ -119,9 +120,12 @@ impl DisallowedPathEnum {
119120 }
120121 }
121122
122- fn allow_invalid ( & self ) -> bool {
123+ fn may_not_refer_to_existing_definition ( & self ) -> bool {
123124 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 ( ) ,
125129 Self :: Simple ( _) => false ,
126130 }
127131 }
@@ -133,6 +137,7 @@ pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>(
133137 tcx : TyCtxt < ' _ > ,
134138 disallowed_paths : & ' static [ DisallowedPath < REPLACEMENT_ALLOWED > ] ,
135139 def_kind_predicate : impl Fn ( DefKind ) -> bool ,
140+ predicate_description : & str ,
136141 allow_prim_tys : bool ,
137142) -> (
138143 DefIdMap < ( & ' static str , & ' static DisallowedPath < REPLACEMENT_ALLOWED > ) > ,
@@ -143,13 +148,13 @@ pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>(
143148 FxHashMap :: default ( ) ;
144149 for disallowed_path in disallowed_paths {
145150 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 < _ > > ( ) ) ;
147152
148- let mut found_def_id = false ;
153+ let mut found_def_id = None ;
149154 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 ) ;
153158 def_kind_predicate ( * def_kind)
154159 } ,
155160 Res :: PrimTy ( _) => {
@@ -159,23 +164,32 @@ pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>(
159164 _ => false ,
160165 } ) ;
161166
162- if reses . is_empty ( ) {
167+ if resolutions . is_empty ( ) {
163168 let span = disallowed_path. span ( ) ;
164169
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+ ) ;
169179 } 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+ ) ;
175189 }
176190 }
177191
178- for res in reses {
192+ for res in resolutions {
179193 match res {
180194 Res :: Def ( _, def_id) => {
181195 def_ids. insert ( def_id, ( path, disallowed_path) ) ;
0 commit comments