Refactor disallowed_methods
:
* Simplify `def_id` extraction. * Use the span of the method name instead of the call.
This commit is contained in:
parent
885f97e2ce
commit
60af2585f7
@ -1,6 +1,6 @@
|
||||
use clippy_config::types::DisallowedPath;
|
||||
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::{Expr, ExprKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
@ -83,26 +83,26 @@ fn check_crate(&mut self, cx: &LateContext<'_>) {
|
||||
}
|
||||
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
let uncalled_path = if let Some(parent) = get_parent_expr(cx, expr)
|
||||
&& let ExprKind::Call(receiver, _) = parent.kind
|
||||
&& receiver.hir_id == expr.hir_id
|
||||
let (id, span) = match &expr.kind {
|
||||
ExprKind::Path(path)
|
||||
if let Res::Def(DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::AssocFn, id) =
|
||||
cx.qpath_res(path, expr.hir_id) =>
|
||||
{
|
||||
None
|
||||
} else {
|
||||
path_def_id(cx, expr)
|
||||
};
|
||||
let Some(def_id) = uncalled_path.or_else(|| fn_def_id(cx, expr)) else {
|
||||
return;
|
||||
};
|
||||
let conf = match self.disallowed.get(&def_id) {
|
||||
Some(&index) => &self.conf_disallowed[index],
|
||||
None => return,
|
||||
(id, expr.span)
|
||||
},
|
||||
ExprKind::MethodCall(name, ..) if let Some(id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) => {
|
||||
(id, name.ident.span)
|
||||
},
|
||||
_ => return,
|
||||
};
|
||||
if let Some(&index) = self.disallowed.get(&id) {
|
||||
let conf = &self.conf_disallowed[index];
|
||||
let msg = format!("use of a disallowed method `{}`", conf.path());
|
||||
span_lint_and_then(cx, DISALLOWED_METHODS, expr.span, msg, |diag| {
|
||||
span_lint_and_then(cx, DISALLOWED_METHODS, span, msg, |diag| {
|
||||
if let Some(reason) = conf.reason() {
|
||||
diag.note(reason);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,18 @@
|
||||
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 | | lint.primary_message(msg);
|
||||
LL | | });
|
||||
| |______^
|
||||
LL | cx.span_lint(lint, span, |lint| {
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
= 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`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]`
|
||||
|
||||
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 | | lint.primary_message(msg);
|
||||
LL | | });
|
||||
| |______^
|
||||
LL | tcx.node_span_lint(lint, hir_id, span, |lint| {
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
= 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)
|
||||
|
||||
|
@ -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
|
||||
|
|
||||
LL | let re = Regex::new(r"ab.*c").unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::disallowed-methods` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]`
|
||||
|
||||
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");
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^
|
||||
|
|
||||
= note: no matching allowed (from clippy.toml)
|
||||
|
||||
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>();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^
|
||||
|
||||
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();
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
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);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^
|
||||
|
||||
error: use of a disallowed method `regex::Regex::new`
|
||||
--> 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
|
||||
|
|
||||
LL | let same_name_as_module = select_all(vec![empty::<()>()]);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^
|
||||
|
||||
error: use of a disallowed method `conf_disallowed_methods::local_fn`
|
||||
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:56:5
|
||||
|
|
||||
LL | local_fn();
|
||||
| ^^^^^^^^^^
|
||||
| ^^^^^^^^
|
||||
|
||||
error: use of a disallowed method `conf_disallowed_methods::local_mod::f`
|
||||
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:57:5
|
||||
|
|
||||
LL | local_mod::f();
|
||||
| ^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^^^
|
||||
|
||||
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();
|
||||
| ^^^^^^^^^^
|
||||
| ^^^^^^
|
||||
|
||||
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();
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
||||
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();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 14 previous errors
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user