Skip to content

Commit adbcf2d

Browse files
committed
Address review comments
1 parent a7edf7d commit adbcf2d

File tree

10 files changed

+57
-43
lines changed

10 files changed

+57
-43
lines changed

clippy.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ lint-commented-code = true
77
[[disallowed-methods]]
88
path = "rustc_lint::context::LintContext::lint"
99
reason = "this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint*` functions instead"
10-
allow-invalid = true
10+
may-not-refer-to-existing-definition = true
1111

1212
[[disallowed-methods]]
1313
path = "rustc_lint::context::LintContext::span_lint"
1414
reason = "this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint*` functions instead"
15-
allow-invalid = true
15+
may-not-refer-to-existing-definition = true
1616

1717
[[disallowed-methods]]
1818
path = "rustc_middle::ty::context::TyCtxt::node_span_lint"
1919
reason = "this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint_hir*` functions instead"
20-
allow-invalid = true
20+
may-not-refer-to-existing-definition = true

clippy_config/src/conf.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ macro_rules! define_Conf {
225225
$(#[doc = $doc:literal])+
226226
$(#[conf_deprecated($dep:literal, $new_conf:ident)])?
227227
$(#[default_text = $default_text:expr])?
228-
$(#[disallowed_path_replacements_allowed = $replacements_allowed:expr])?
228+
$(#[disallowed_paths_allow_replacements = $replacements_allowed:expr])?
229229
$(#[lints($($for_lints:ident),* $(,)?)])?
230230
$name:ident: $ty:ty = $default:expr,
231231
)*) => {
@@ -526,7 +526,7 @@ define_Conf! {
526526
)]
527527
avoid_breaking_exported_api: bool = true,
528528
/// The list of types which may not be held across an await point.
529-
#[disallowed_path_replacements_allowed = false]
529+
#[disallowed_paths_allow_replacements = false]
530530
#[lints(await_holding_invalid_type)]
531531
await_holding_invalid_types: Vec<DisallowedPathWithoutReplacement> = Vec::new(),
532532
/// DEPRECATED LINT: BLACKLISTED_NAME.
@@ -572,11 +572,11 @@ define_Conf! {
572572
#[conf_deprecated("Please use `cognitive-complexity-threshold` instead", cognitive_complexity_threshold)]
573573
cyclomatic_complexity_threshold: u64 = 25,
574574
/// The list of disallowed macros, written as fully qualified paths.
575-
#[disallowed_path_replacements_allowed = true]
575+
#[disallowed_paths_allow_replacements = true]
576576
#[lints(disallowed_macros)]
577577
disallowed_macros: Vec<DisallowedPath> = Vec::new(),
578578
/// The list of disallowed methods, written as fully qualified paths.
579-
#[disallowed_path_replacements_allowed = true]
579+
#[disallowed_paths_allow_replacements = true]
580580
#[lints(disallowed_methods)]
581581
disallowed_methods: Vec<DisallowedPath> = Vec::new(),
582582
/// The list of disallowed names to lint about. NB: `bar` is not here since it has legitimate uses. The value
@@ -585,7 +585,7 @@ define_Conf! {
585585
#[lints(disallowed_names)]
586586
disallowed_names: Vec<String> = DEFAULT_DISALLOWED_NAMES.iter().map(ToString::to_string).collect(),
587587
/// The list of disallowed types, written as fully qualified paths.
588-
#[disallowed_path_replacements_allowed = true]
588+
#[disallowed_paths_allow_replacements = true]
589589
#[lints(disallowed_types)]
590590
disallowed_types: Vec<DisallowedPath> = Vec::new(),
591591
/// The list of words this lint should not consider as identifiers needing ticks. The value

clippy_config/src/types.rs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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));

clippy_lints/src/await_holding_invalid.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ impl AwaitHolding {
183183
tcx,
184184
&conf.await_holding_invalid_types,
185185
crate::disallowed_types::def_kind_predicate,
186+
"type",
186187
false,
187188
);
188189
Self { def_ids }

clippy_lints/src/disallowed_macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl DisallowedMacros {
7676
tcx,
7777
&conf.disallowed_macros,
7878
|def_kind| matches!(def_kind, DefKind::Macro(_)),
79+
"macro",
7980
false,
8081
);
8182
Self {

clippy_lints/src/disallowed_methods.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ impl DisallowedMethods {
7272
DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::AssocFn
7373
)
7474
},
75+
"function",
7576
false,
7677
);
7778
Self { disallowed }
@@ -83,9 +84,6 @@ impl_lint_pass!(DisallowedMethods => [DISALLOWED_METHODS]);
8384
impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
8485
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
8586
let (id, span) = match &expr.kind {
86-
// Previous versions of this lint checked the `DefKind` in the next arm. However, that is no longer
87-
// necessary. Paths are filtered by `DefKind` when `self.disallowed` is created. So if a path is not of an
88-
// appropriate `DefKind`, `self.disallowed.get(..)` will return `None` below.
8987
ExprKind::Path(path) if let Res::Def(_, id) = cx.qpath_res(path, expr.hir_id) => (id, expr.span),
9088
ExprKind::MethodCall(name, ..) if let Some(id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) => {
9189
(id, name.ident.span)

clippy_lints/src/disallowed_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub struct DisallowedTypes {
6060

6161
impl DisallowedTypes {
6262
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
63-
let (def_ids, prim_tys) = create_disallowed_map(tcx, &conf.disallowed_types, def_kind_predicate, true);
63+
let (def_ids, prim_tys) = create_disallowed_map(tcx, &conf.disallowed_types, def_kind_predicate, "type", true);
6464
Self { def_ids, prim_tys }
6565
}
6666

tests/ui-toml/toml_invalid_path/clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ path = "std::process::current_exe"
1111

1212
[[disallowed-methods]]
1313
path = "std::current_exe"
14-
allow-invalid = true
14+
may-not-refer-to-existing-definition = true
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//@error-in-other-file: primitive types are not allowed: bool
2-
//@error-in-other-file: `std::process::current_exe` is invalid
3-
//@error-in-other-file: `std::result::Result::Err` is not of an appropriate kind
1+
//@error-in-other-file: expected a macro, found a primitive type
2+
//@error-in-other-file: `std::process::current_exe` does not refer to an existing function
3+
//@error-in-other-file: expected a type, found a tuple variant
44

55
fn main() {}

tests/ui-toml/toml_invalid_path/conf_invalid_path.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
warning: primitive types are not allowed: bool
1+
warning: expected a macro, found a primitive type
22
--> $DIR/tests/ui-toml/toml_invalid_path/clippy.toml:4:1
33
|
44
LL | / [[disallowed-macros]]
55
LL | | path = "bool"
66
| |_____________^
77

8-
warning: `std::process::current_exe` is invalid
8+
warning: `std::process::current_exe` does not refer to an existing function
99
--> $DIR/tests/ui-toml/toml_invalid_path/clippy.toml:7:1
1010
|
1111
LL | / [[disallowed-methods]]
1212
LL | | path = "std::process::current_exe"
1313
| |__________________________________^
1414

15-
warning: `std::result::Result::Err` is not of an appropriate kind
15+
warning: expected a type, found a tuple variant
1616
--> $DIR/tests/ui-toml/toml_invalid_path/clippy.toml:1:1
1717
|
1818
LL | / [[disallowed-types]]

0 commit comments

Comments
 (0)