Refactor disallowed_methods:

* Simplify `def_id` extraction.
* Use the span of the method name instead of the call.
This commit is contained in:
Jason Newcomb 2024-06-09 16:26:26 -04:00
parent 885f97e2ce
commit 60af2585f7
3 changed files with 45 additions and 49 deletions

View File

@ -1,6 +1,6 @@
use clippy_config::types::DisallowedPath; use clippy_config::types::DisallowedPath;
use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::{fn_def_id, get_parent_expr, path_def_id}; use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::DefIdMap; use rustc_hir::def_id::DefIdMap;
use rustc_hir::{Expr, ExprKind}; use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
@ -83,26 +83,26 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
} }
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
let uncalled_path = if let Some(parent) = get_parent_expr(cx, expr) let (id, span) = match &expr.kind {
&& let ExprKind::Call(receiver, _) = parent.kind ExprKind::Path(path)
&& receiver.hir_id == expr.hir_id if let Res::Def(DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::AssocFn, id) =
{ cx.qpath_res(path, expr.hir_id) =>
None {
} else { (id, expr.span)
path_def_id(cx, expr) },
ExprKind::MethodCall(name, ..) if let Some(id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) => {
(id, name.ident.span)
},
_ => return,
}; };
let Some(def_id) = uncalled_path.or_else(|| fn_def_id(cx, expr)) else { if let Some(&index) = self.disallowed.get(&id) {
return; let conf = &self.conf_disallowed[index];
}; let msg = format!("use of a disallowed method `{}`", conf.path());
let conf = match self.disallowed.get(&def_id) { span_lint_and_then(cx, DISALLOWED_METHODS, span, msg, |diag| {
Some(&index) => &self.conf_disallowed[index], if let Some(reason) = conf.reason() {
None => return, diag.note(reason);
}; }
let msg = format!("use of a disallowed method `{}`", conf.path()); });
span_lint_and_then(cx, DISALLOWED_METHODS, expr.span, msg, |diag| { }
if let Some(reason) = conf.reason() {
diag.note(reason);
}
});
} }
} }

View File

@ -1,22 +1,18 @@
error: use of a disallowed method `rustc_lint::context::LintContext::span_lint` error: use of a disallowed method `rustc_lint::context::LintContext::span_lint`
--> tests/ui-internal/disallow_span_lint.rs:14:5 --> tests/ui-internal/disallow_span_lint.rs:14:8
| |
LL | / cx.span_lint(lint, span, |lint| { LL | cx.span_lint(lint, span, |lint| {
LL | | lint.primary_message(msg); | ^^^^^^^^^
LL | | });
| |______^
| |
= note: this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint*` functions instead (from clippy.toml) = note: this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint*` functions instead (from clippy.toml)
= note: `-D clippy::disallowed-methods` implied by `-D warnings` = note: `-D clippy::disallowed-methods` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]` = help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]`
error: use of a disallowed method `rustc_middle::ty::context::TyCtxt::node_span_lint` error: use of a disallowed method `rustc_middle::ty::context::TyCtxt::node_span_lint`
--> tests/ui-internal/disallow_span_lint.rs:20:5 --> tests/ui-internal/disallow_span_lint.rs:20:9
| |
LL | / tcx.node_span_lint(lint, hir_id, span, |lint| { LL | tcx.node_span_lint(lint, hir_id, span, |lint| {
LL | | lint.primary_message(msg); | ^^^^^^^^^^^^^^
LL | | });
| |______^
| |
= 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) = 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)

View File

@ -2,36 +2,36 @@ error: use of a disallowed method `regex::Regex::new`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:35:14 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:35:14
| |
LL | let re = Regex::new(r"ab.*c").unwrap(); LL | let re = Regex::new(r"ab.*c").unwrap();
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^
| |
= note: `-D clippy::disallowed-methods` implied by `-D warnings` = note: `-D clippy::disallowed-methods` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]` = help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]`
error: use of a disallowed method `regex::Regex::is_match` error: use of a disallowed method `regex::Regex::is_match`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:36:5 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:36:8
| |
LL | re.is_match("abc"); LL | re.is_match("abc");
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^
| |
= note: no matching allowed (from clippy.toml) = note: no matching allowed (from clippy.toml)
error: use of a disallowed method `std::iter::Iterator::sum` error: use of a disallowed method `std::iter::Iterator::sum`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:39:5 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:39:14
| |
LL | a.iter().sum::<i32>(); LL | a.iter().sum::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^
error: use of a disallowed method `slice::sort_unstable` error: use of a disallowed method `slice::sort_unstable`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:41:5 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:41:7
| |
LL | a.sort_unstable(); LL | a.sort_unstable();
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
error: use of a disallowed method `f32::clamp` error: use of a disallowed method `f32::clamp`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:44:13 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:44:20
| |
LL | let _ = 2.0f32.clamp(3.0f32, 4.0f32); LL | let _ = 2.0f32.clamp(3.0f32, 4.0f32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^
error: use of a disallowed method `regex::Regex::new` error: use of a disallowed method `regex::Regex::new`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:47:61 --> 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`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:54:31 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:54:31
| |
LL | let same_name_as_module = select_all(vec![empty::<()>()]); LL | let same_name_as_module = select_all(vec![empty::<()>()]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^
error: use of a disallowed method `conf_disallowed_methods::local_fn` error: use of a disallowed method `conf_disallowed_methods::local_fn`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:56:5 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:56:5
| |
LL | local_fn(); LL | local_fn();
| ^^^^^^^^^^ | ^^^^^^^^
error: use of a disallowed method `conf_disallowed_methods::local_mod::f` error: use of a disallowed method `conf_disallowed_methods::local_mod::f`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:57:5 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:57:5
| |
LL | local_mod::f(); LL | local_mod::f();
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^
error: use of a disallowed method `conf_disallowed_methods::Struct::method` error: use of a disallowed method `conf_disallowed_methods::Struct::method`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:59:5 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:59:7
| |
LL | s.method(); LL | s.method();
| ^^^^^^^^^^ | ^^^^^^
error: use of a disallowed method `conf_disallowed_methods::Trait::provided_method` error: use of a disallowed method `conf_disallowed_methods::Trait::provided_method`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:60:5 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:60:7
| |
LL | s.provided_method(); LL | s.provided_method();
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: use of a disallowed method `conf_disallowed_methods::Trait::implemented_method` error: use of a disallowed method `conf_disallowed_methods::Trait::implemented_method`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:61:5 --> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:61:7
| |
LL | s.implemented_method(); LL | s.implemented_method();
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
error: aborting due to 14 previous errors error: aborting due to 14 previous errors