Skip to content

Commit 04eb27a

Browse files
committed
Use method name from conf::DisallowedMethod
Since def_path_str returns e.g. "core::f32::<impl f32>::clamp" for "f32::clamp"
1 parent 91581f6 commit 04eb27a

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

clippy_lints/src/disallowed_methods.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ declare_clippy_lint! {
5959
#[derive(Clone, Debug)]
6060
pub struct DisallowedMethods {
6161
conf_disallowed: Vec<conf::DisallowedMethod>,
62-
disallowed: DefIdMap<Option<String>>,
62+
disallowed: DefIdMap<usize>,
6363
}
6464

6565
impl DisallowedMethods {
@@ -75,17 +75,10 @@ impl_lint_pass!(DisallowedMethods => [DISALLOWED_METHODS]);
7575

7676
impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
7777
fn check_crate(&mut self, cx: &LateContext<'_>) {
78-
for conf in &self.conf_disallowed {
79-
let (path, reason) = match conf {
80-
conf::DisallowedMethod::Simple(path) => (path, None),
81-
conf::DisallowedMethod::WithReason { path, reason } => (
82-
path,
83-
reason.as_ref().map(|reason| format!("{} (from clippy.toml)", reason)),
84-
),
85-
};
86-
let segs: Vec<_> = path.split("::").collect();
78+
for (index, conf) in self.conf_disallowed.iter().enumerate() {
79+
let segs: Vec<_> = conf.path().split("::").collect();
8780
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &segs) {
88-
self.disallowed.insert(id, reason);
81+
self.disallowed.insert(id, index);
8982
}
9083
}
9184
}
@@ -95,15 +88,17 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
9588
Some(def_id) => def_id,
9689
None => return,
9790
};
98-
let reason = match self.disallowed.get(&def_id) {
99-
Some(reason) => reason,
91+
let conf = match self.disallowed.get(&def_id) {
92+
Some(&index) => &self.conf_disallowed[index],
10093
None => return,
10194
};
102-
let func_path = cx.tcx.def_path_str(def_id);
103-
let msg = format!("use of a disallowed method `{}`", func_path);
95+
let msg = format!("use of a disallowed method `{}`", conf.path());
10496
span_lint_and_then(cx, DISALLOWED_METHODS, expr.span, &msg, |diag| {
105-
if let Some(reason) = reason {
106-
diag.note(reason);
97+
if let conf::DisallowedMethod::WithReason {
98+
reason: Some(reason), ..
99+
} = conf
100+
{
101+
diag.note(&format!("{} (from clippy.toml)", reason));
107102
}
108103
});
109104
}

clippy_lints/src/utils/conf.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ pub enum DisallowedMethod {
2323
WithReason { path: String, reason: Option<String> },
2424
}
2525

26+
impl DisallowedMethod {
27+
pub fn path(&self) -> &str {
28+
let (Self::Simple(path) | Self::WithReason { path, .. }) = self;
29+
30+
path
31+
}
32+
}
33+
2634
/// A single disallowed type, used by the `DISALLOWED_TYPES` lint.
2735
#[derive(Clone, Debug, Deserialize)]
2836
#[serde(untagged)]

tests/ui-toml/toml_disallowed_methods/clippy.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
disallowed-methods = [
22
# just a string is shorthand for path only
33
"std::iter::Iterator::sum",
4+
"f32::clamp",
5+
"slice::sort_unstable",
46
# can give path and reason with an inline table
57
{ path = "regex::Regex::is_match", reason = "no matching allowed" },
68
# can use an inline table but omit reason

tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ fn main() {
77
let re = Regex::new(r"ab.*c").unwrap();
88
re.is_match("abc");
99

10-
let a = vec![1, 2, 3, 4];
10+
let mut a = vec![1, 2, 3, 4];
1111
a.iter().sum::<i32>();
12+
13+
a.sort_unstable();
14+
15+
let _ = 2.0f32.clamp(3.0f32, 4.0f32);
16+
let _ = 2.0f64.clamp(3.0f64, 4.0f64);
1217
}

tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,17 @@ error: use of a disallowed method `std::iter::Iterator::sum`
2020
LL | a.iter().sum::<i32>();
2121
| ^^^^^^^^^^^^^^^^^^^^^
2222

23-
error: aborting due to 3 previous errors
23+
error: use of a disallowed method `slice::sort_unstable`
24+
--> $DIR/conf_disallowed_methods.rs:13:5
25+
|
26+
LL | a.sort_unstable();
27+
| ^^^^^^^^^^^^^^^^^
28+
29+
error: use of a disallowed method `f32::clamp`
30+
--> $DIR/conf_disallowed_methods.rs:15:13
31+
|
32+
LL | let _ = 2.0f32.clamp(3.0f32, 4.0f32);
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
35+
error: aborting due to 5 previous errors
2436

0 commit comments

Comments
 (0)