Skip to content

Warn if unresolved disallowed types/macros/methods are used in clippy.toml for disallowed_* macros #10090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions clippy_lints/src/disallowed_macros.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::macros::macro_backtrace;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::DefIdMap;
use rustc_hir::{Expr, ForeignItem, HirId, ImplItem, Item, Pat, Path, Stmt, TraitItem, Ty};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::DUMMY_SP;
use rustc_span::{ExpnId, Span};

use crate::utils::conf;
Expand Down Expand Up @@ -103,8 +104,20 @@ impl LateLintPass<'_> for DisallowedMacros {
fn check_crate(&mut self, cx: &LateContext<'_>) {
for (index, conf) in self.conf_disallowed.iter().enumerate() {
let segs: Vec<_> = conf.path().split("::").collect();
for id in clippy_utils::def_path_def_ids(cx, &segs) {
self.disallowed.insert(id, index);
let ids = clippy_utils::def_path_def_ids(cx, &segs).collect::<Vec<_>>();

if ids.is_empty() {
// The path couldn't be resolved to anything
span_lint(
cx,
DISALLOWED_MACROS,
DUMMY_SP,
&format!("Could not resolve disallowed macro: `{}`", conf.path()),
);
} else {
for id in ids {
self.disallowed.insert(id, index);
}
}
}
}
Expand Down
19 changes: 16 additions & 3 deletions clippy_lints/src/disallowed_methods.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::{fn_def_id, get_parent_expr, path_def_id};

use rustc_hir::def_id::DefIdMap;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::DUMMY_SP;

use crate::utils::conf;

Expand Down Expand Up @@ -78,8 +79,20 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
fn check_crate(&mut self, cx: &LateContext<'_>) {
for (index, conf) in self.conf_disallowed.iter().enumerate() {
let segs: Vec<_> = conf.path().split("::").collect();
for id in clippy_utils::def_path_def_ids(cx, &segs) {
self.disallowed.insert(id, index);
let ids = clippy_utils::def_path_def_ids(cx, &segs).collect::<Vec<_>>();

if ids.is_empty() {
// The path couldn't be resolved to anything
span_lint(
cx,
DISALLOWED_METHODS,
DUMMY_SP,
&format!("Could not resolve disallowed method: `{}`", conf.path()),
);
} else {
for id in ids {
self.disallowed.insert(id, index);
}
}
}
}
Expand Down
32 changes: 22 additions & 10 deletions clippy_lints/src/disallowed_types.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};

use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::Res;
use rustc_hir::def_id::DefId;
use rustc_hir::{Item, ItemKind, PolyTraitRef, PrimTy, Ty, TyKind, UseKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::DUMMY_SP;
use rustc_span::Span;

use crate::utils::conf;
Expand Down Expand Up @@ -89,16 +90,27 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedTypes {
fn check_crate(&mut self, cx: &LateContext<'_>) {
for (index, conf) in self.conf_disallowed.iter().enumerate() {
let segs: Vec<_> = conf.path().split("::").collect();
let reses = clippy_utils::def_path_res(cx, &segs);

for res in clippy_utils::def_path_res(cx, &segs) {
match res {
Res::Def(_, id) => {
self.def_ids.insert(id, index);
},
Res::PrimTy(ty) => {
self.prim_tys.insert(ty, index);
},
_ => {},
if reses.is_empty() {
// The path couldn't be resolved to anything
span_lint(
cx,
DISALLOWED_TYPES,
DUMMY_SP,
&format!("Could not resolve disallowed type: `{}`", conf.path()),
);
} else {
for res in reses {
match res {
Res::Def(_, id) => {
self.def_ids.insert(id, index);
},
Res::PrimTy(ty) => {
self.prim_tys.insert(ty, index);
},
_ => {},
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ui-toml/disallowed_macros/clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ disallowed-macros = [
"macros::ty",
"macros::pat",
"macros::item",
# Macros which don't exist will lint
"disallowed_macros::does_not_exist",
]
2 changes: 1 addition & 1 deletion tests/ui-toml/disallowed_macros/disallowed_macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// aux-build:macros.rs
// --crate-name disallowed_macros aux-build:macros.rs

#![allow(unused)]

Expand Down
8 changes: 5 additions & 3 deletions tests/ui-toml/disallowed_macros/disallowed_macros.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
error: Could not resolve disallowed macro: `disallowed_macros::does_not_exist`
|
= note: `-D clippy::disallowed-macros` implied by `-D warnings`

error: use of a disallowed macro `std::println`
--> $DIR/disallowed_macros.rs:10:5
|
LL | println!("one");
| ^^^^^^^^^^^^^^^
|
= note: `-D clippy::disallowed-macros` implied by `-D warnings`

error: use of a disallowed macro `std::println`
--> $DIR/disallowed_macros.rs:11:5
Expand Down Expand Up @@ -80,5 +82,5 @@ error: use of a disallowed macro `macros::item`
LL | macros::item!();
| ^^^^^^^^^^^^^^^

error: aborting due to 13 previous errors
error: aborting due to 14 previous errors

2 changes: 2 additions & 0 deletions tests/ui-toml/toml_disallowed_methods/clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ disallowed-methods = [
"conf_disallowed_methods::Struct::method",
"conf_disallowed_methods::Trait::provided_method",
"conf_disallowed_methods::Trait::implemented_method",
# Methods which don't exist will lint
"conf_disallowed_methods::does_not_exist",
]
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
error: Could not resolve disallowed method: `conf_disallowed_methods::does_not_exist`
|
= note: `-D clippy::disallowed-methods` implied by `-D warnings`

error: use of a disallowed method `regex::Regex::new`
--> $DIR/conf_disallowed_methods.rs:33:14
|
LL | let re = Regex::new(r"ab.*c").unwrap();
| ^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::disallowed-methods` implied by `-D warnings`

error: use of a disallowed method `regex::Regex::is_match`
--> $DIR/conf_disallowed_methods.rs:34:5
Expand Down Expand Up @@ -86,5 +88,5 @@ error: use of a disallowed method `conf_disallowed_methods::Trait::implemented_m
LL | s.implemented_method();
| ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 14 previous errors
error: aborting due to 15 previous errors

2 changes: 2 additions & 0 deletions tests/ui-toml/toml_disallowed_types/clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ disallowed-types = [
{ path = "std::net::Ipv4Addr", reason = "no IPv4 allowed" },
# can use an inline table but omit reason
{ path = "std::net::TcpListener" },
# Types which don't exist will lint
"conf_disallowed_types::DoesNotExist",
]
2 changes: 2 additions & 0 deletions tests/ui-toml/toml_disallowed_types/conf_disallowed_types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// compile-flags: --crate-name conf_disallowed_types

#![warn(clippy::disallowed_types)]

extern crate quote;
Expand Down
50 changes: 26 additions & 24 deletions tests/ui-toml/toml_disallowed_types/conf_disallowed_types.stderr
Original file line number Diff line number Diff line change
@@ -1,132 +1,134 @@
error: Could not resolve disallowed type: `conf_disallowed_types::DoesNotExist`
|
= note: `-D clippy::disallowed-types` implied by `-D warnings`

error: `std::sync::atomic::AtomicU32` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:7:1
--> $DIR/conf_disallowed_types.rs:9:1
|
LL | use std::sync::atomic::AtomicU32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::disallowed-types` implied by `-D warnings`

error: `std::time::Instant` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:8:1
--> $DIR/conf_disallowed_types.rs:10:1
|
LL | use std::time::Instant as Sneaky;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `std::time::Instant` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:12:33
--> $DIR/conf_disallowed_types.rs:14:33
|
LL | fn bad_return_type() -> fn() -> Sneaky {
| ^^^^^^

error: `std::time::Instant` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:16:28
--> $DIR/conf_disallowed_types.rs:18:28
|
LL | fn bad_arg_type(_: impl Fn(Sneaky) -> foo::atomic::AtomicU32) {}
| ^^^^^^

error: `std::sync::atomic::AtomicU32` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:16:39
--> $DIR/conf_disallowed_types.rs:18:39
|
LL | fn bad_arg_type(_: impl Fn(Sneaky) -> foo::atomic::AtomicU32) {}
| ^^^^^^^^^^^^^^^^^^^^^^

error: `std::io::Read` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:18:22
--> $DIR/conf_disallowed_types.rs:20:22
|
LL | fn trait_obj(_: &dyn std::io::Read) {}
| ^^^^^^^^^^^^^

error: `usize` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:20:33
--> $DIR/conf_disallowed_types.rs:22:33
|
LL | fn full_and_single_path_prim(_: usize, _: bool) {}
| ^^^^^

error: `bool` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:20:43
--> $DIR/conf_disallowed_types.rs:22:43
|
LL | fn full_and_single_path_prim(_: usize, _: bool) {}
| ^^^^

error: `usize` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:22:28
--> $DIR/conf_disallowed_types.rs:24:28
|
LL | fn const_generics<const C: usize>() {}
| ^^^^^

error: `usize` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:24:24
--> $DIR/conf_disallowed_types.rs:26:24
|
LL | struct GenArg<const U: usize>([u8; U]);
| ^^^^^

error: `std::net::Ipv4Addr` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:28:10
--> $DIR/conf_disallowed_types.rs:30:10
|
LL | fn ip(_: std::net::Ipv4Addr) {}
| ^^^^^^^^^^^^^^^^^^
|
= note: no IPv4 allowed (from clippy.toml)

error: `std::net::TcpListener` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:30:16
--> $DIR/conf_disallowed_types.rs:32:16
|
LL | fn listener(_: std::net::TcpListener) {}
| ^^^^^^^^^^^^^^^^^^^^^

error: `std::collections::HashMap` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:34:48
--> $DIR/conf_disallowed_types.rs:36:48
|
LL | let _: std::collections::HashMap<(), ()> = std::collections::HashMap::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: `std::collections::HashMap` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:34:12
--> $DIR/conf_disallowed_types.rs:36:12
|
LL | let _: std::collections::HashMap<(), ()> = std::collections::HashMap::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `std::time::Instant` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:35:13
--> $DIR/conf_disallowed_types.rs:37:13
|
LL | let _ = Sneaky::now();
| ^^^^^^

error: `std::sync::atomic::AtomicU32` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:36:13
--> $DIR/conf_disallowed_types.rs:38:13
|
LL | let _ = foo::atomic::AtomicU32::new(0);
| ^^^^^^^^^^^^^^^^^^^^^^

error: `std::sync::atomic::AtomicU32` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:37:17
--> $DIR/conf_disallowed_types.rs:39:17
|
LL | static FOO: std::sync::atomic::AtomicU32 = foo::atomic::AtomicU32::new(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `std::sync::atomic::AtomicU32` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:37:48
--> $DIR/conf_disallowed_types.rs:39:48
|
LL | static FOO: std::sync::atomic::AtomicU32 = foo::atomic::AtomicU32::new(1);
| ^^^^^^^^^^^^^^^^^^^^^^

error: `syn::TypePath` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:38:43
--> $DIR/conf_disallowed_types.rs:40:43
|
LL | let _: std::collections::BTreeMap<(), syn::TypePath> = Default::default();
| ^^^^^^^^^^^^^

error: `syn::Ident` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:39:13
--> $DIR/conf_disallowed_types.rs:41:13
|
LL | let _ = syn::Ident::new("", todo!());
| ^^^^^^^^^^

error: `usize` is not allowed according to config
--> $DIR/conf_disallowed_types.rs:41:12
--> $DIR/conf_disallowed_types.rs:43:12
|
LL | let _: usize = 64_usize;
| ^^^^^

error: aborting due to 21 previous errors
error: aborting due to 22 previous errors