Skip to content

Commit 5e6676c

Browse files
committed
Added the rest of the suggested changes.
1 parent 6b87037 commit 5e6676c

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

clippy_lints/src/methods/join_absolute_paths.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::macros::HirNode;
23
use clippy_utils::ty::is_type_diagnostic_item;
34
use rustc_ast::ast::LitKind;
45
use rustc_errors::Applicability;
56
use rustc_hir::{Expr, ExprKind};
67
use rustc_lint::LateContext;
78
use rustc_span::symbol::sym::Path;
8-
use rustc_span::Span;
99

1010
use super::JOIN_ABSOLUTE_PATHS;
1111

12-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>, span: Span) {
13-
let ty = cx.typeck_results().expr_ty(expr);
12+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>) {
13+
let ty = cx.typeck_results().expr_ty(expr).peel_refs();
1414
if !expr.span.from_expansion() {
1515
if is_type_diagnostic_item(cx, ty, Path) {
1616
let applicability = Applicability::MachineApplicable;
@@ -20,8 +20,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_a
2020
span_lint_and_sugg(
2121
cx,
2222
JOIN_ABSOLUTE_PATHS,
23-
span.with_hi(expr.span.hi()),
24-
r#"argument in join called on path contains a starting '/'"#,
23+
join_arg.span().with_hi(expr.span.hi()),
24+
r#"argument to `Path::join` starts with a path separator"#,
2525
"try removing first '/' or '\\'",
2626
"join(\"your/path/here\")".to_owned(),
2727
applicability,

clippy_lints/src/methods/mod.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,8 +2975,8 @@ declare_clippy_lint! {
29752975
/// # let mut state = DefaultHasher::new();
29762976
/// # let my_enum = Foo::Empty;
29772977
/// match my_enum {
2978-
/// Empty => ().hash(&mut state),
2979-
/// WithValue(x) => x.hash(&mut state),
2978+
/// Empty => ().hash(&mut state),
2979+
/// WithValue(x) => x.hash(&mut state),
29802980
/// }
29812981
/// ```
29822982
/// Use instead:
@@ -2988,8 +2988,8 @@ declare_clippy_lint! {
29882988
/// # let mut state = DefaultHasher::new();
29892989
/// # let my_enum = Foo::Empty;
29902990
/// match my_enum {
2991-
/// Empty => 0_u8.hash(&mut state),
2992-
/// WithValue(x) => x.hash(&mut state),
2991+
/// Empty => 0_u8.hash(&mut state),
2992+
/// WithValue(x) => x.hash(&mut state),
29932993
/// }
29942994
/// ```
29952995
#[clippy::version = "1.58.0"]
@@ -3264,7 +3264,7 @@ declare_clippy_lint! {
32643264

32653265
declare_clippy_lint! {
32663266
/// ### What it does
3267-
/// Checks for initial `'/ or \\'` in an argument to `.join()` on a `Path`.
3267+
/// Checks for calls to `Path::join` that start with a path separator, like `\\` or `/`..
32683268
///
32693269
/// ### Why is this bad?
32703270
/// `.join()` comments starting with a separator (`/` or `\\`) can replace the entire path.
@@ -3281,20 +3281,21 @@ declare_clippy_lint! {
32813281
///
32823282
/// Use instead;
32833283
/// ```rust
3284-
/// let path = std::path::Path::new("/bin");
3284+
/// # use std::path::{Path, PathBuf};
3285+
/// let path = Path::new("/bin");
32853286
///
32863287
/// // If this was unintentional, remove the leading separator
3287-
/// let extend = path.join("sh");
3288-
/// assert_eq!(extend, std::path::PathBuf::from("/bin/sh"));
3288+
/// let joined_path = path.join("sh");
3289+
/// assert_eq!(joined_path, PathBuf::from("/bin/sh"));
32893290
///
32903291
/// // If this was intentional, create a new path instead
3291-
/// let new = std::path::Path::new("/sh");
3292-
/// assert_eq!(new, std::path::PathBuf::from("/sh"));
3292+
/// let new = Path::new("/sh");
3293+
/// assert_eq!(new, PathBuf::from("/sh"));
32933294
/// ```
32943295
#[clippy::version = "1.70.0"]
32953296
pub JOIN_ABSOLUTE_PATHS,
32963297
pedantic,
3297-
"arg to .join called on a Path contains '/' at the start"
3298+
"arg to .join called on a Path contains separator at the start"
32983299
}
32993300

33003301
declare_clippy_lint! {
@@ -3869,7 +3870,9 @@ impl Methods {
38693870
if let Some(("collect", _, _, span, _)) = method_call(recv) {
38703871
unnecessary_join::check(cx, expr, recv, join_arg, span);
38713872
}
3872-
else {join_absolute_paths::check(cx, expr, join_arg, span);}
3873+
else {
3874+
join_absolute_paths::check(cx, recv, join_arg);
3875+
}
38733876
},
38743877
("last", []) | ("skip", [_]) => {
38753878
if let Some((name2, recv2, args2, _span2, _)) = method_call(recv) {

tests/ui/join_absolute_paths.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use std::path::Path;
66
fn main() {
77
// should be linted
88
let path = Path::new("/bin");
9-
path.join("/sh");
9+
pathjoin("your/path/here")"/sh");
1010
println!("{}", path.display());
1111

1212
//should be linted
1313
let path = Path::new("C:\\Users");
14-
path.join("\\user");
14+
pathjoin("your/path/here")"\\user");
1515
println!("{}", path.display());
1616

1717
// should not be linted

tests/ui/join_absolute_paths.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//@run-rustfix
21
#![allow(unused)]
32
#![warn(clippy::join_absolute_paths)]
43
use std::path::Path;

tests/ui/join_absolute_paths.stderr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: argument to `Path::join` starts with a path separator
2+
--> $DIR/join_absolute_paths.rs:8:9
3+
|
4+
LL | path.join("/sh");
5+
| ^^^^^^ help: try removing first '/' or '/': `join("your/path/here")`
6+
|
7+
= note: `-D clippy::join-absolute-paths` implied by `-D warnings`
8+
9+
error: argument to `Path::join` starts with a path separator
10+
--> $DIR/join_absolute_paths.rs:13:9
11+
|
12+
LL | path.join("//user");
13+
| ^^^^^^ help: try removing first '/' or '/': `join("your/path/here")`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)