forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Frank
committed
Sep 25, 2020
1 parent
cc1998f
commit 1479c18
Showing
10 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::utils::span_lint; | ||
|
||
use rustc_data_structures::fx::FxHashSet; | ||
use rustc_lint::{LateLintPass, LateContext}; | ||
use rustc_session::{impl_lint_pass, declare_tool_lint}; | ||
use rustc_hir::*; | ||
use rustc_span::Symbol; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Lints for specific trait methods defined in clippy.toml | ||
/// | ||
/// **Why is this bad?** Some methods are undesirable in certain contexts, | ||
/// and it would be beneficial to lint for them as needed. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// // example code where clippy issues a warning | ||
/// foo.bad_method(); // Foo is disallowed | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// // example code which does not raise clippy warning | ||
/// GoodStruct.bad_method(); // not disallowed | ||
/// ``` | ||
pub DISALLOWED_METHOD, | ||
nursery, | ||
"used disallowed method call" | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct DisallowedMethod { | ||
disallowed: FxHashSet<Vec<Symbol>>, | ||
} | ||
|
||
impl DisallowedMethod { | ||
pub fn new(disallowed: FxHashSet<String>) -> Self { | ||
Self { | ||
disallowed: disallowed.iter() | ||
.map(|s| { | ||
s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>() | ||
}) | ||
.collect(), | ||
} | ||
} | ||
} | ||
|
||
impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]); | ||
|
||
impl <'tcx> LateLintPass<'tcx> for DisallowedMethod { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
if let ExprKind::MethodCall(path, _, _args, _) = &expr.kind { | ||
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap(); | ||
|
||
let method_call = cx.get_def_path(def_id); | ||
if self.disallowed.contains(&method_call) { | ||
span_lint( | ||
cx, | ||
DISALLOWED_METHOD, | ||
expr.span, | ||
&format!( | ||
"Use of a disallowed method `{}`", | ||
method_call | ||
.iter() | ||
.map(|s| s.to_ident_string()) | ||
.collect::<Vec<_>>() | ||
.join("::"), | ||
) | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
disallowed-methods = ["core::iter::traits::iterator::Iterator::sum", "regex::re_unicode::Regex::is_match"] |
13 changes: 13 additions & 0 deletions
13
tests/ui-toml/toml_disallowed_method/conf_disallowed_method.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![warn(clippy::disallowed_method)] | ||
|
||
extern crate regex; | ||
use regex::Regex; | ||
|
||
fn main() { | ||
let a = vec![1, 2, 3, 4]; | ||
let re = Regex::new(r"ab.*c").unwrap(); | ||
|
||
re.is_match("abc"); | ||
|
||
a.iter().sum::<i32>(); | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/ui-toml/toml_disallowed_method/conf_disallowed_method.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error: Use of a disallowed method `regex::re_unicode::Regex::is_match` | ||
--> $DIR/conf_disallowed_method.rs:10:5 | ||
| | ||
LL | re.is_match("abc"); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::disallowed-method` implied by `-D warnings` | ||
|
||
error: Use of a disallowed method `core::iter::traits::iterator::Iterator::sum` | ||
--> $DIR/conf_disallowed_method.rs:12:5 | ||
| | ||
LL | a.iter().sum::<i32>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#![warn(clippy::disallowed_method)] | ||
#![allow(clippy::no_effect, clippy::many_single_char_names)] | ||
|
||
struct ImplStruct; | ||
|
||
trait Baz { | ||
fn bad_method(self); | ||
} | ||
|
||
impl Baz for ImplStruct { | ||
fn bad_method(self) {} | ||
} | ||
|
||
struct Foo; | ||
|
||
impl Foo { | ||
fn bad_method(self) {} | ||
} | ||
|
||
struct StaticStruct; | ||
|
||
trait Quux { | ||
fn bad_method(); | ||
} | ||
|
||
impl Quux for StaticStruct { | ||
fn bad_method() {} | ||
} | ||
|
||
struct NormalStruct; | ||
|
||
impl NormalStruct { | ||
fn bad_method(self) {} | ||
} | ||
|
||
struct AttrStruct { | ||
bad_method: i32, | ||
} | ||
|
||
fn main() { | ||
let b = ImplStruct; | ||
let f = Foo; | ||
let c = ImplStruct; | ||
let n = NormalStruct; | ||
let a = AttrStruct{ bad_method: 5 }; | ||
|
||
// lint these | ||
b.bad_method(); | ||
c.bad_method(); | ||
f.bad_method(); | ||
// these are good | ||
// good because not a method call (ExprKind => Call) | ||
StaticStruct::bad_method(); | ||
n.bad_method(); | ||
a.bad_method; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: Use of a disallowed method `disallowed_method::Baz::bad_method` | ||
--> $DIR/disallowed_method.rs:48:5 | ||
| | ||
LL | b.bad_method(); | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::disallowed-method` implied by `-D warnings` | ||
|
||
error: Use of a disallowed method `disallowed_method::Baz::bad_method` | ||
--> $DIR/disallowed_method.rs:49:5 | ||
| | ||
LL | c.bad_method(); | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: Use of a disallowed method `disallowed_method::Foo::bad_method` | ||
--> $DIR/disallowed_method.rs:50:5 | ||
| | ||
LL | f.bad_method(); | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|