Skip to content

Commit f26f117

Browse files
committed
Auto merge of rust-lang#8852 - Alexendoo:indirect-disallowed-methods, r=Manishearth
Lint indirect usages in `disallowed_methods` Fixes rust-lang#8849 changelog: Lint indirect usages in [`disallowed_methods`]
2 parents 6f26383 + 5730fd4 commit f26f117

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

clippy_lints/src/disallowed_methods.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::fn_def_id;
2+
use clippy_utils::{fn_def_id, get_parent_expr, path_def_id};
33

4-
use rustc_hir::{def::Res, def_id::DefIdMap, Expr};
4+
use rustc_hir::{def::Res, def_id::DefIdMap, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_tool_lint, impl_lint_pass};
77

@@ -84,7 +84,15 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
8484
}
8585

8686
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
87-
let def_id = match fn_def_id(cx, expr) {
87+
let uncalled_path = if let Some(parent) = get_parent_expr(cx, expr)
88+
&& let ExprKind::Call(receiver, _) = parent.kind
89+
&& receiver.hir_id == expr.hir_id
90+
{
91+
None
92+
} else {
93+
path_def_id(cx, expr)
94+
};
95+
let def_id = match uncalled_path.or_else(|| fn_def_id(cx, expr)) {
8896
Some(def_id) => def_id,
8997
None => return,
9098
};

tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs

+6
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ fn main() {
1414

1515
let _ = 2.0f32.clamp(3.0f32, 4.0f32);
1616
let _ = 2.0f64.clamp(3.0f64, 4.0f64);
17+
18+
let indirect: fn(&str) -> Result<Regex, regex::Error> = Regex::new;
19+
let re = indirect(".").unwrap();
20+
21+
let in_call = Box::new(f32::clamp);
22+
let in_method_call = ["^", "$"].into_iter().map(Regex::new);
1723
}

tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,23 @@ error: use of a disallowed method `f32::clamp`
3232
LL | let _ = 2.0f32.clamp(3.0f32, 4.0f32);
3333
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3434

35-
error: aborting due to 5 previous errors
35+
error: use of a disallowed method `regex::Regex::new`
36+
--> $DIR/conf_disallowed_methods.rs:18:61
37+
|
38+
LL | let indirect: fn(&str) -> Result<Regex, regex::Error> = Regex::new;
39+
| ^^^^^^^^^^
40+
41+
error: use of a disallowed method `f32::clamp`
42+
--> $DIR/conf_disallowed_methods.rs:21:28
43+
|
44+
LL | let in_call = Box::new(f32::clamp);
45+
| ^^^^^^^^^^
46+
47+
error: use of a disallowed method `regex::Regex::new`
48+
--> $DIR/conf_disallowed_methods.rs:22:53
49+
|
50+
LL | let in_method_call = ["^", "$"].into_iter().map(Regex::new);
51+
| ^^^^^^^^^^
52+
53+
error: aborting due to 8 previous errors
3654

0 commit comments

Comments
 (0)