Skip to content

Commit 62fd9a5

Browse files
Mason Ray Hanna IIIofeeg
authored andcommitted
Added type check and test case for string slice.
1 parent 851b58d commit 62fd9a5

File tree

4 files changed

+57
-29
lines changed

4 files changed

+57
-29
lines changed

clippy_lints/src/methods/path_join_correction.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<<<<<<< HEAD
2+
<<<<<<< HEAD
23
use clippy_utils::{diagnostics::span_lint_and_sugg, ty::is_type_diagnostic_item};
34
use rustc_ast::ast::LitKind;
45
use rustc_errors::Applicability;
@@ -30,11 +31,14 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_a
3031
);
3132
=======
3233
use clippy_utils::diagnostics::span_lint_and_sugg;
34+
=======
35+
use clippy_utils::{diagnostics::span_lint_and_sugg, ty::is_type_diagnostic_item};
36+
>>>>>>> 00ee1bf71 (Added type check and test case for string slice.)
3337
use rustc_ast::ast::LitKind;
3438
use rustc_errors::Applicability;
3539
use rustc_hir::{Expr, ExprKind};
3640
use rustc_lint::LateContext;
37-
use rustc_span::Span;
41+
use rustc_span::{symbol::sym::Path, Span};
3842

3943
use super::PATH_JOIN_CORRECTION;
4044

@@ -66,22 +70,30 @@ pub(super) fn check<'tcx>(
6670
>>>>>>> 9048d99eb (Added new lint for issue #10655)
6771
=======
6872
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>, span: Span) {
69-
let applicability = Applicability::MachineApplicable;
70-
if_chain!(
71-
if let ExprKind::Lit(spanned) = &join_arg.kind;
72-
if let LitKind::Str(symbol, _) = spanned.node;
73-
if symbol.as_str().starts_with('/');
74-
then {
75-
span_lint_and_sugg(
76-
cx,
77-
PATH_JOIN_CORRECTION,
78-
span.with_hi(expr.span.hi()),
79-
r#"argument in join called on path contains a starting '/'"#,
80-
"try removing first '/'",
81-
"join(\"your/path/here\")".to_owned(),
82-
applicability
73+
let ty = cx.typeck_results().expr_ty(expr);
74+
if is_type_diagnostic_item(cx, ty, Path) {
75+
let applicability = Applicability::MachineApplicable;
76+
if_chain!(
77+
if let ExprKind::Lit(spanned) = &join_arg.kind;
78+
if let LitKind::Str(symbol, _) = spanned.node;
79+
if symbol.as_str().starts_with('/');
80+
then {
81+
span_lint_and_sugg(
82+
cx,
83+
PATH_JOIN_CORRECTION,
84+
span.with_hi(expr.span.hi()),
85+
r#"argument in join called on path contains a starting '/'"#,
86+
"try removing first '/'",
87+
"join(\"your/path/here\")".to_owned(),
88+
applicability
89+
);
90+
}
8391
);
92+
<<<<<<< HEAD
8493
}
8594
);
8695
>>>>>>> b87b08b12 (Fixed errors and lack of documentation in mod.rs.)
96+
=======
97+
}
98+
>>>>>>> 00ee1bf71 (Added type check and test case for string slice.)
8799
}

tests/ui/path_join_correction.fixed

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(unused)]
33
#![warn(clippy::path_join_correction)]
44
<<<<<<< HEAD
5+
<<<<<<< HEAD
56
use std::path::Path;
67

78
fn main() {
@@ -23,16 +24,29 @@ fn main() {
2324
//should not be linted
2425
let path = Path::new("/bin");
2526
=======
27+
=======
28+
use std::path::Path;
29+
//use std::string::String;
30+
>>>>>>> 00ee1bf71 (Added type check and test case for string slice.)
2631

2732
fn main() {
2833
// should be linted
29-
let path = std::path::Path::new("/bin");
30-
path.join("your/path/here");
34+
let path = Path::new("/bin");
35+
path.join("/sh");
3136
println!("{}", path.display());
3237

38+
// should not be linted
39+
let path: &[&str] = &["/bin"];
40+
path.join("/sh");
41+
println!("{:?}", path);
42+
3343
//should not be linted
44+
<<<<<<< HEAD
3445
let path = std::path::Path::new("/bin");
3546
>>>>>>> b87b08b12 (Fixed errors and lack of documentation in mod.rs.)
47+
=======
48+
let path = Path::new("/bin");
49+
>>>>>>> 00ee1bf71 (Added type check and test case for string slice.)
3650
path.join("sh");
3751
println!("{}", path.display());
3852
}

tests/ui/path_join_correction.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(unused)]
55
#![warn(clippy::path_join_correction)]
66
use std::path::Path;
7+
<<<<<<< HEAD
78

89
fn main() {
910
// should be linted
@@ -31,13 +32,17 @@ fn main() {
3132
>>>>>>> b87b08b12 (Fixed errors and lack of documentation in mod.rs.)
3233
#![allow(unused)]
3334
#![warn(clippy::path_join_correction)]
35+
=======
36+
//use std::string::String;
37+
>>>>>>> 00ee1bf71 (Added type check and test case for string slice.)
3438

3539
fn main() {
3640
// should be linted
37-
let path = std::path::Path::new("/bin");
41+
let path = Path::new("/bin");
3842
path.join("/sh");
3943
println!("{}", path.display());
4044

45+
<<<<<<< HEAD
4146
<<<<<<< HEAD
4247
//should not be linted
4348
let path = std::path::Path::new("/bin");
@@ -46,8 +51,15 @@ fn main() {
4651

4752
>>>>>>> 9048d99eb (Added new lint for issue #10655)
4853
=======
54+
=======
55+
// should not be linted
56+
let path: &[&str] = &["/bin"];
57+
path.join("/sh");
58+
println!("{:?}", path);
59+
60+
>>>>>>> 00ee1bf71 (Added type check and test case for string slice.)
4961
//should not be linted
50-
let path = std::path::Path::new("/bin");
62+
let path = Path::new("/bin");
5163
path.join("sh");
5264
println!("{}", path.display());
5365
>>>>>>> b87b08b12 (Fixed errors and lack of documentation in mod.rs.)

tests/ui/path_join_correction.stderr

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)