Skip to content

Commit 327cc32

Browse files
author
Mason Ray Hanna III
committed
Fixed errors and lack of documentation in mod.rs.
1 parent 5de0ee9 commit 327cc32

File tree

5 files changed

+53
-45
lines changed

5 files changed

+53
-45
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
mod path_join_correction;
21
mod bind_instead_of_map;
32
mod bytecount;
43
mod bytes_count_to_len;
@@ -69,6 +68,7 @@ mod option_map_unwrap_or;
6968
mod or_fun_call;
7069
mod or_then_unwrap;
7170
mod path_buf_push_overwrite;
71+
mod path_join_correction;
7272
mod range_zip_with_len;
7373
mod repeat_once;
7474
mod search_is_some;
@@ -3219,21 +3219,23 @@ declare_clippy_lint! {
32193219

32203220
declare_clippy_lint! {
32213221
/// ### What it does
3222-
///
3222+
/// Checks for initial '/' in an argument to .join on a path.
32233223
/// ### Why is this bad?
3224-
///
3224+
/// .join() calls on paths already attach the '/'.
32253225
/// ### Example
32263226
/// ```rust
32273227
/// // example code where clippy issues a warning
3228+
/// let path = std::path::Path::new("/bin");
3229+
/// path.join("/sh");
32283230
/// ```
32293231
/// Use instead:
32303232
/// ```rust
3231-
/// // example code which does not raise clippy warning
3233+
/// // path.join("sh");
32323234
/// ```
32333235
#[clippy::version = "1.70.0"]
32343236
pub PATH_JOIN_CORRECTION,
32353237
pedantic,
3236-
"default lint description"
3238+
"arg to .join called on a Path contains '/' at the start"
32373239
}
32383240

32393241
pub struct Methods {
@@ -3678,9 +3680,6 @@ impl Methods {
36783680
}
36793681
else {path_join_correction::check(cx, expr, join_arg, span);}
36803682
},
3681-
/*("join", [join_arg]) => {
3682-
path_join_correction::check(cx, expr, join_arg, span);
3683-
},*/
36843683
("last", []) | ("skip", [_]) => {
36853684
if let Some((name2, recv2, args2, _span2, _)) = method_call(recv) {
36863685
if let ("cloned", []) = (name2, args2) {
Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use rustc_lint::{LateContext};
32
use rustc_ast::ast::LitKind;
43
use rustc_errors::Applicability;
54
use rustc_hir::{Expr, ExprKind};
5+
use rustc_lint::LateContext;
66
use rustc_span::Span;
77

88
use super::PATH_JOIN_CORRECTION;
99

10-
11-
pub(super) fn check<'tcx>(
12-
cx: &LateContext<'tcx>,
13-
expr: &'tcx Expr<'tcx>,
14-
join_arg: &'tcx Expr<'tcx>,
15-
span: Span,
16-
) {
17-
let applicability = Applicability::MachineApplicable;
18-
if_chain!(
19-
if let ExprKind::Lit(spanned) = &join_arg.kind;
20-
if let LitKind::Str(symbol, _) = spanned.node;
21-
if symbol.as_str().starts_with('/');
22-
then {
23-
span_lint_and_sugg(
24-
cx,
25-
PATH_JOIN_CORRECTION,
26-
span.with_hi(expr.span.hi()),
27-
r#"argument in join called on path contains a starting '/'"#,
28-
"try removing first '/'",
29-
"join(\"your/path/here\")".to_owned(),
30-
applicability
31-
);
32-
}
33-
);
10+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>, span: Span) {
11+
let applicability = Applicability::MachineApplicable;
12+
if_chain!(
13+
if let ExprKind::Lit(spanned) = &join_arg.kind;
14+
if let LitKind::Str(symbol, _) = spanned.node;
15+
if symbol.as_str().starts_with('/');
16+
then {
17+
span_lint_and_sugg(
18+
cx,
19+
PATH_JOIN_CORRECTION,
20+
span.with_hi(expr.span.hi()),
21+
r#"argument in join called on path contains a starting '/'"#,
22+
"try removing first '/'",
23+
"join(\"your/path/here\")".to_owned(),
24+
applicability
25+
);
26+
}
27+
);
3428
}

tests/ui/path_join_correction.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-rustfix
2+
#![allow(unused)]
3+
#![warn(clippy::path_join_correction)]
4+
5+
fn main() {
6+
// should be linted
7+
let path = std::path::Path::new("/bin");
8+
path.join("your/path/here");
9+
println!("{}", path.display());
10+
11+
//should not be linted
12+
let path = std::path::Path::new("/bin");
13+
path.join("sh");
14+
println!("{}", path.display());
15+
}

tests/ui/path_join_correction.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
// run-rustfix
12
#![allow(unused)]
23
#![warn(clippy::path_join_correction)]
34

45
fn main() {
5-
// should be linted
6-
let path = std::path::Path::new("/bin");
7-
path.join("/sh");
8-
println!("{}", path.display());
6+
// should be linted
7+
let path = std::path::Path::new("/bin");
8+
path.join("/sh");
9+
println!("{}", path.display());
910

10-
//should not be linted
11-
let path = std::path::Path::new("/bin");
12-
path.join("sh");
13-
println!("{}", path.display());
14-
11+
//should not be linted
12+
let path = std::path::Path::new("/bin");
13+
path.join("sh");
14+
println!("{}", path.display());
1515
}

tests/ui/path_join_correction.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: argument in join called on path contains a starting '/'
2-
--> $DIR/path_join_correction.rs:7:8
2+
--> $DIR/path_join_correction.rs:8:10
33
|
4-
LL | path.join("/sh");
5-
| ^^^^^^^^^^^ help: try removing first '/': `join("your/path/here")`
4+
LL | path.join("/sh");
5+
| ^^^^^^^^^^^ help: try removing first '/': `join("your/path/here")`
66
|
77
= note: `-D clippy::path-join-correction` implied by `-D warnings`
88

0 commit comments

Comments
 (0)