forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new lint for issue rust-lang#10655
- Loading branch information
Mason Ray Hanna III
committed
Apr 17, 2023
1 parent
90cb0fa
commit 9a7bf41
Showing
6 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use rustc_lint::{LateContext}; | ||
use rustc_ast::ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_span::Span; | ||
|
||
use super::PATH_JOIN_CORRECTION; | ||
|
||
// TODO: Adjust the parameters as necessary | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
join_arg: &'tcx Expr<'tcx>, | ||
span: Span, | ||
) { | ||
let applicability = Applicability::MachineApplicable; | ||
if_chain!( | ||
if let ExprKind::Lit(spanned) = &join_arg.kind; | ||
if let LitKind::Str(symbol, _) = spanned.node; | ||
if symbol.as_str().starts_with('/'); | ||
then { | ||
span_lint_and_sugg( | ||
cx, | ||
PATH_JOIN_CORRECTION, | ||
span.with_hi(expr.span.hi()), | ||
r#"argument in join called on path contains a starting '/'"#, | ||
"try removing first '/'", | ||
"join(\"your/path/here\")".to_owned(), | ||
applicability | ||
); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::path_join_correction)] | ||
|
||
fn main() { | ||
// should be linted | ||
let path = std::path::Path::new("/bin"); | ||
path.join("/sh"); | ||
println!("{}", path.display()); | ||
|
||
//should not be linted | ||
let path = std::path::Path::new("/bin"); | ||
path.join("sh"); | ||
println!("{}", path.display()); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: argument in join called on path contains a starting '/' | ||
--> $DIR/path_join_correction.rs:7:8 | ||
| | ||
LL | path.join("/sh"); | ||
| ^^^^^^^^^^^ help: try removing first '/': `join("your/path/here")` | ||
| | ||
= note: `-D clippy::path-join-correction` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|