Skip to content

Commit 9ae3fcb

Browse files
committed
implemeted lint, but the other lint now fails expectedly
1 parent 368b235 commit 9ae3fcb

File tree

7 files changed

+103
-0
lines changed

7 files changed

+103
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6643,6 +6643,7 @@ Released 2018-09-13
66436643
[`useless_format`]: https://rust-lang.github.io/rust-clippy/master/index.html#useless_format
66446644
[`useless_let_if_seq`]: https://rust-lang.github.io/rust-clippy/master/index.html#useless_let_if_seq
66456645
[`useless_nonzero_new_unchecked`]: https://rust-lang.github.io/rust-clippy/master/index.html#useless_nonzero_new_unchecked
6646+
[`useless_patbuf_conversion`]: https://rust-lang.github.io/rust-clippy/master/index.html#useless_patbuf_conversion
66466647
[`useless_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#useless_transmute
66476648
[`useless_vec`]: https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec
66486649
[`vec_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#vec_box

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
773773
crate::use_self::USE_SELF_INFO,
774774
crate::useless_concat::USELESS_CONCAT_INFO,
775775
crate::useless_conversion::USELESS_CONVERSION_INFO,
776+
crate::useless_pathbuf_conversion::USELESS_PATHBUF_CONVERSION_INFO,
776777
crate::vec::USELESS_VEC_INFO,
777778
crate::vec_init_then_push::VEC_INIT_THEN_PUSH_INFO,
778779
crate::visibility::NEEDLESS_PUB_SELF_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ mod upper_case_acronyms;
398398
mod use_self;
399399
mod useless_concat;
400400
mod useless_conversion;
401+
mod useless_pathbuf_conversion;
401402
mod vec;
402403
mod vec_init_then_push;
403404
mod visibility;
@@ -581,6 +582,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
581582
store.register_late_pass(|_| Box::new(infinite_iter::InfiniteIter));
582583
store.register_late_pass(|_| Box::new(inline_fn_without_body::InlineFnWithoutBody));
583584
store.register_late_pass(|_| Box::<useless_conversion::UselessConversion>::default());
585+
store.register_late_pass(|_| Box::new(useless_pathbuf_conversion::UselessPathbufConversion));
584586
store.register_late_pass(|_| Box::new(implicit_hasher::ImplicitHasher));
585587
store.register_late_pass(|_| Box::new(fallible_impl_from::FallibleImplFrom));
586588
store.register_late_pass(move |_| Box::new(question_mark::QuestionMark::new(conf)));
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet;
3+
use clippy_utils::ty::is_type_diagnostic_item;
4+
use rustc_hir::{Expr, ExprKind};
5+
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_session::declare_lint_pass;
7+
use rustc_span::symbol::sym;
8+
9+
declare_clippy_lint! {
10+
/// **What it does:** Detects unnecessary `&PathBuf::from(...)` when `&Path` would suffice.
11+
///
12+
/// **Why is this bad?** `PathBuf::from` allocates a new heap buffer unnecessarily.
13+
///
14+
/// **Example:**
15+
/// ```rust
16+
/// fn use_path(p: &std::path::Path) {}
17+
/// use_path(&std::path::PathBuf::from("abc"));
18+
/// ```
19+
/// Could be written as:
20+
/// ```rust
21+
/// fn use_path(p: &std::path::Path) {}
22+
/// use_path(std::path::Path::new("abc"));
23+
/// ```
24+
#[clippy::version = "1.91.0"]
25+
pub USELESS_PATHBUF_CONVERSION,
26+
complexity,
27+
"creating a PathBuf only to take a reference, where Path::new would suffice"
28+
}
29+
30+
declare_lint_pass!(UselessPathbufConversion => [USELESS_PATHBUF_CONVERSION]);
31+
32+
impl<'tcx> LateLintPass<'tcx> for UselessPathbufConversion {
33+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
34+
// Only care about &PathBuf::from(...)
35+
if let ExprKind::AddrOf(_, _, inner) = &expr.kind {
36+
if let ExprKind::Call(func, args) = &inner.kind {
37+
if let ExprKind::Path(ref qpath) = func.kind {
38+
if let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() {
39+
// check that the function is `from`
40+
if cx.tcx.item_name(def_id) != sym::from {
41+
return;
42+
}
43+
44+
// get the type of the function's return value
45+
let ty = cx.typeck_results().expr_ty(inner);
46+
47+
if is_type_diagnostic_item(cx, ty, sym::PathBuf) {
48+
if let Some(arg) = args.get(0) {
49+
let sugg = format!("Path::new({})", snippet(cx, arg.span, ".."));
50+
span_lint_and_sugg(
51+
cx,
52+
USELESS_PATHBUF_CONVERSION,
53+
expr.span,
54+
"unnecessary `PathBuf::from` when a `&Path` is enough",
55+
"consider using",
56+
sugg,
57+
rustc_errors::Applicability::MachineApplicable,
58+
);
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![warn(clippy::useless_pathbuf_conversion)]
2+
use std::path::{Path, PathBuf};
3+
4+
fn use_path(p: &Path) {}
5+
6+
fn main() {
7+
use_path(Path::new("abc"));
8+
//~^ useless_pathbuf_conversion
9+
10+
use_path(Path::new("abc"));
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![warn(clippy::useless_pathbuf_conversion)]
2+
use std::path::{Path, PathBuf};
3+
4+
fn use_path(p: &Path) {}
5+
6+
fn main() {
7+
use_path(&PathBuf::from("abc"));
8+
//~^ useless_pathbuf_conversion
9+
10+
use_path(Path::new("abc"));
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: unnecessary `PathBuf::from` when a `&Path` is enough
2+
--> tests/ui/useless_pathbuf_conversion.rs:7:14
3+
|
4+
LL | use_path(&PathBuf::from("abc"));
5+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `Path::new("abc")`
6+
|
7+
= note: `-D clippy::useless-pathbuf-conversion` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::useless_pathbuf_conversion)]`
9+
10+
error: aborting due to 1 previous error
11+

0 commit comments

Comments
 (0)