Skip to content

Commit 84862cf

Browse files
committed
test: function stored in a variable
1 parent 69537dc commit 84862cf

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

clippy_lints/src/needless_path_new.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::path_res;
32
use clippy_utils::source::snippet;
3+
use clippy_utils::{expr_or_init, path_res};
44
use rustc_errors::Applicability;
55
use rustc_hir::def::{CtorKind, DefKind, Res};
66
use rustc_hir::{Expr, ExprKind, QPath};
@@ -59,7 +59,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPathNew {
5959
let (fn_did, args) = match e.kind {
6060
ExprKind::Call(callee, args)
6161
if let Res::Def(DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn), did) =
62-
path_res(cx, callee) =>
62+
// re: `expr_or_init`: `callee` might be a variable storing a fn ptr, for example,
63+
// so we need to get to the actual initializer
64+
path_res(cx, expr_or_init(cx, callee)) =>
6365
{
6466
(did, args)
6567
},

tests/ui/needless_path_new.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::path::Path;
55

66
fn takes_path(_: &Path) {}
77

8+
fn takes_impl_path(_: impl AsRef<Path>) {}
9+
810
fn takes_path_and_impl_path(_: &Path, _: impl AsRef<Path>) {}
911

1012
fn takes_two_impl_paths_with_the_same_generic<P: AsRef<Path>>(_: P, _: P) {}
@@ -42,6 +44,10 @@ fn main() {
4244
"bar", //~ needless_path_new
4345
);
4446

47+
let a = takes_impl_path;
48+
49+
a("foo.txt"); //~ needless_path_new
50+
4551
// no warning
4652
takes_path(Path::new("foo"));
4753

tests/ui/needless_path_new.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::path::Path;
55

66
fn takes_path(_: &Path) {}
77

8+
fn takes_impl_path(_: impl AsRef<Path>) {}
9+
810
fn takes_path_and_impl_path(_: &Path, _: impl AsRef<Path>) {}
911

1012
fn takes_two_impl_paths_with_the_same_generic<P: AsRef<Path>>(_: P, _: P) {}
@@ -42,6 +44,10 @@ fn main() {
4244
Path::new("bar"), //~ needless_path_new
4345
);
4446

47+
let a = takes_impl_path;
48+
49+
a(Path::new("foo.txt")); //~ needless_path_new
50+
4551
// no warning
4652
takes_path(Path::new("foo"));
4753

tests/ui/needless_path_new.stderr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@ LL | fs::write(Path::new("foo.txt"), "foo");
77
= note: `-D clippy::needless-path-new` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::needless_path_new)]`
99

10+
error: the expression enclosed in `Path::new` implements `AsRef<Path>`
11+
--> tests/ui/needless_path_new.rs:31:9
12+
|
13+
LL | Path::new("bar"),
14+
| ^^^^^^^^^^^^^^^^ help: remove the enclosing `Path::new`: `"bar"`
15+
1016
error: the expression enclosed in `Path::new` implements `AsRef<Path>`
1117
--> tests/ui/needless_path_new.rs:30:9
1218
|
1319
LL | Path::new("foo"),
1420
| ^^^^^^^^^^^^^^^^ help: remove the enclosing `Path::new`: `"foo"`
1521

1622
error: the expression enclosed in `Path::new` implements `AsRef<Path>`
17-
--> tests/ui/needless_path_new.rs:31:9
23+
--> tests/ui/needless_path_new.rs:38:9
1824
|
1925
LL | Path::new("bar"),
2026
| ^^^^^^^^^^^^^^^^ help: remove the enclosing `Path::new`: `"bar"`
2127

2228
error: the expression enclosed in `Path::new` implements `AsRef<Path>`
23-
--> tests/ui/needless_path_new.rs:38:9
29+
--> tests/ui/needless_path_new.rs:44:9
2430
|
2531
LL | Path::new("bar"),
2632
| ^^^^^^^^^^^^^^^^ help: remove the enclosing `Path::new`: `"bar"`
@@ -32,10 +38,10 @@ LL | Path::new("foo"),
3238
| ^^^^^^^^^^^^^^^^ help: remove the enclosing `Path::new`: `"foo"`
3339

3440
error: the expression enclosed in `Path::new` implements `AsRef<Path>`
35-
--> tests/ui/needless_path_new.rs:44:9
41+
--> tests/ui/needless_path_new.rs:49:7
3642
|
37-
LL | Path::new("bar"),
38-
| ^^^^^^^^^^^^^^^^ help: remove the enclosing `Path::new`: `"bar"`
43+
LL | a(Path::new("foo.txt"));
44+
| ^^^^^^^^^^^^^^^^^^^^ help: remove the enclosing `Path::new`: `"foo.txt"`
3945

40-
error: aborting due to 6 previous errors
46+
error: aborting due to 7 previous errors
4147

0 commit comments

Comments
 (0)