Skip to content

Commit 4361151

Browse files
author
Mason Ray Hanna III
committed
Added type check and test case for string slice.
1 parent 327cc32 commit 4361151

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::{diagnostics::span_lint_and_sugg, ty::is_type_diagnostic_item};
22
use rustc_ast::ast::LitKind;
33
use rustc_errors::Applicability;
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::LateContext;
6-
use rustc_span::Span;
6+
use rustc_span::{symbol::sym::Path, Span};
77

88
use super::PATH_JOIN_CORRECTION;
99

1010
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
11+
let ty = cx.typeck_results().expr_ty(expr);
12+
if is_type_diagnostic_item(cx, ty, Path) {
13+
let applicability = Applicability::MachineApplicable;
14+
if_chain!(
15+
if let ExprKind::Lit(spanned) = &join_arg.kind;
16+
if let LitKind::Str(symbol, _) = spanned.node;
17+
if symbol.as_str().starts_with('/');
18+
then {
19+
span_lint_and_sugg(
20+
cx,
21+
PATH_JOIN_CORRECTION,
22+
span.with_hi(expr.span.hi()),
23+
r#"argument in join called on path contains a starting '/'"#,
24+
"try removing first '/'",
25+
"join(\"your/path/here\")".to_owned(),
26+
applicability
27+
);
28+
}
2529
);
26-
}
27-
);
30+
}
2831
}

tests/ui/path_join_correction.fixed

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
// run-rustfix
22
#![allow(unused)]
33
#![warn(clippy::path_join_correction)]
4+
use std::path::Path;
5+
//use std::string::String;
46

57
fn main() {
68
// should be linted
7-
let path = std::path::Path::new("/bin");
8-
path.join("your/path/here");
9+
let path = Path::new("/bin");
10+
path.join("/sh");
911
println!("{}", path.display());
1012

13+
// should not be linted
14+
let path: &[&str] = &["/bin"];
15+
path.join("/sh");
16+
println!("{:?}", path);
17+
1118
//should not be linted
12-
let path = std::path::Path::new("/bin");
19+
let path = Path::new("/bin");
1320
path.join("sh");
1421
println!("{}", path.display());
1522
}

tests/ui/path_join_correction.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
// run-rustfix
22
#![allow(unused)]
33
#![warn(clippy::path_join_correction)]
4+
use std::path::Path;
5+
//use std::string::String;
46

57
fn main() {
68
// should be linted
7-
let path = std::path::Path::new("/bin");
9+
let path = Path::new("/bin");
810
path.join("/sh");
911
println!("{}", path.display());
1012

13+
// should not be linted
14+
let path: &[&str] = &["/bin"];
15+
path.join("/sh");
16+
println!("{:?}", path);
17+
1118
//should not be linted
12-
let path = std::path::Path::new("/bin");
19+
let path = Path::new("/bin");
1320
path.join("sh");
1421
println!("{}", path.display());
1522
}

tests/ui/path_join_correction.stderr

-10
This file was deleted.

0 commit comments

Comments
 (0)