Skip to content

Commit 60af258

Browse files
committed
Refactor disallowed_methods:
* Simplify `def_id` extraction. * Use the span of the method name instead of the call.
1 parent 885f97e commit 60af258

File tree

3 files changed

+45
-49
lines changed

3 files changed

+45
-49
lines changed

clippy_lints/src/disallowed_methods.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_config::types::DisallowedPath;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::{fn_def_id, get_parent_expr, path_def_id};
3+
use rustc_hir::def::{CtorKind, DefKind, Res};
44
use rustc_hir::def_id::DefIdMap;
55
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
@@ -83,26 +83,26 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
8383
}
8484

8585
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
86-
let uncalled_path = if let Some(parent) = get_parent_expr(cx, expr)
87-
&& let ExprKind::Call(receiver, _) = parent.kind
88-
&& receiver.hir_id == expr.hir_id
89-
{
90-
None
91-
} else {
92-
path_def_id(cx, expr)
86+
let (id, span) = match &expr.kind {
87+
ExprKind::Path(path)
88+
if let Res::Def(DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::AssocFn, id) =
89+
cx.qpath_res(path, expr.hir_id) =>
90+
{
91+
(id, expr.span)
92+
},
93+
ExprKind::MethodCall(name, ..) if let Some(id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) => {
94+
(id, name.ident.span)
95+
},
96+
_ => return,
9397
};
94-
let Some(def_id) = uncalled_path.or_else(|| fn_def_id(cx, expr)) else {
95-
return;
96-
};
97-
let conf = match self.disallowed.get(&def_id) {
98-
Some(&index) => &self.conf_disallowed[index],
99-
None => return,
100-
};
101-
let msg = format!("use of a disallowed method `{}`", conf.path());
102-
span_lint_and_then(cx, DISALLOWED_METHODS, expr.span, msg, |diag| {
103-
if let Some(reason) = conf.reason() {
104-
diag.note(reason);
105-
}
106-
});
98+
if let Some(&index) = self.disallowed.get(&id) {
99+
let conf = &self.conf_disallowed[index];
100+
let msg = format!("use of a disallowed method `{}`", conf.path());
101+
span_lint_and_then(cx, DISALLOWED_METHODS, span, msg, |diag| {
102+
if let Some(reason) = conf.reason() {
103+
diag.note(reason);
104+
}
105+
});
106+
}
107107
}
108108
}

tests/ui-internal/disallow_span_lint.stderr

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
error: use of a disallowed method `rustc_lint::context::LintContext::span_lint`
2-
--> tests/ui-internal/disallow_span_lint.rs:14:5
2+
--> tests/ui-internal/disallow_span_lint.rs:14:8
33
|
4-
LL | / cx.span_lint(lint, span, |lint| {
5-
LL | | lint.primary_message(msg);
6-
LL | | });
7-
| |______^
4+
LL | cx.span_lint(lint, span, |lint| {
5+
| ^^^^^^^^^
86
|
97
= note: this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint*` functions instead (from clippy.toml)
108
= note: `-D clippy::disallowed-methods` implied by `-D warnings`
119
= help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]`
1210

1311
error: use of a disallowed method `rustc_middle::ty::context::TyCtxt::node_span_lint`
14-
--> tests/ui-internal/disallow_span_lint.rs:20:5
12+
--> tests/ui-internal/disallow_span_lint.rs:20:9
1513
|
16-
LL | / tcx.node_span_lint(lint, hir_id, span, |lint| {
17-
LL | | lint.primary_message(msg);
18-
LL | | });
19-
| |______^
14+
LL | tcx.node_span_lint(lint, hir_id, span, |lint| {
15+
| ^^^^^^^^^^^^^^
2016
|
2117
= note: this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint_hir*` functions instead (from clippy.toml)
2218

tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@ error: use of a disallowed method `regex::Regex::new`
22
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:35:14
33
|
44
LL | let re = Regex::new(r"ab.*c").unwrap();
5-
| ^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^
66
|
77
= note: `-D clippy::disallowed-methods` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]`
99

1010
error: use of a disallowed method `regex::Regex::is_match`
11-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:36:5
11+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:36:8
1212
|
1313
LL | re.is_match("abc");
14-
| ^^^^^^^^^^^^^^^^^^
14+
| ^^^^^^^^
1515
|
1616
= note: no matching allowed (from clippy.toml)
1717

1818
error: use of a disallowed method `std::iter::Iterator::sum`
19-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:39:5
19+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:39:14
2020
|
2121
LL | a.iter().sum::<i32>();
22-
| ^^^^^^^^^^^^^^^^^^^^^
22+
| ^^^
2323

2424
error: use of a disallowed method `slice::sort_unstable`
25-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:41:5
25+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:41:7
2626
|
2727
LL | a.sort_unstable();
28-
| ^^^^^^^^^^^^^^^^^
28+
| ^^^^^^^^^^^^^
2929

3030
error: use of a disallowed method `f32::clamp`
31-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:44:13
31+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:44:20
3232
|
3333
LL | let _ = 2.0f32.clamp(3.0f32, 4.0f32);
34-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
| ^^^^^
3535

3636
error: use of a disallowed method `regex::Regex::new`
3737
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:47:61
@@ -55,37 +55,37 @@ error: use of a disallowed method `futures::stream::select_all`
5555
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:54:31
5656
|
5757
LL | let same_name_as_module = select_all(vec![empty::<()>()]);
58-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
58+
| ^^^^^^^^^^
5959

6060
error: use of a disallowed method `conf_disallowed_methods::local_fn`
6161
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:56:5
6262
|
6363
LL | local_fn();
64-
| ^^^^^^^^^^
64+
| ^^^^^^^^
6565

6666
error: use of a disallowed method `conf_disallowed_methods::local_mod::f`
6767
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:57:5
6868
|
6969
LL | local_mod::f();
70-
| ^^^^^^^^^^^^^^
70+
| ^^^^^^^^^^^^
7171

7272
error: use of a disallowed method `conf_disallowed_methods::Struct::method`
73-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:59:5
73+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:59:7
7474
|
7575
LL | s.method();
76-
| ^^^^^^^^^^
76+
| ^^^^^^
7777

7878
error: use of a disallowed method `conf_disallowed_methods::Trait::provided_method`
79-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:60:5
79+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:60:7
8080
|
8181
LL | s.provided_method();
82-
| ^^^^^^^^^^^^^^^^^^^
82+
| ^^^^^^^^^^^^^^^
8383

8484
error: use of a disallowed method `conf_disallowed_methods::Trait::implemented_method`
85-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:61:5
85+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:61:7
8686
|
8787
LL | s.implemented_method();
88-
| ^^^^^^^^^^^^^^^^^^^^^^
88+
| ^^^^^^^^^^^^^^^^^^
8989

9090
error: aborting due to 14 previous errors
9191

0 commit comments

Comments
 (0)