-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed if chain. Added the rest of the suggested changes. Applied cargo test supposed changes. Resolved rest of changes. Resolved nitpicks. Update clippy_lints/src/methods/join_absolute_paths.rs Co-authored-by: Catherine Flores <[email protected]> Formatted and blessed. Got rid of println! calls.
- Loading branch information
Showing
8 changed files
with
84 additions
and
62 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 was deleted.
Oops, something went wrong.
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,30 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_ast::ast::LitKind; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym::Path; | ||
|
||
use super::JOIN_ABSOLUTE_PATHS; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>) { | ||
let ty = cx.typeck_results().expr_ty(expr).peel_refs(); | ||
if is_type_diagnostic_item(cx, ty, Path) | ||
&& let ExprKind::Lit(spanned) = &join_arg.kind | ||
&& let LitKind::Str(symbol, _) = spanned.node | ||
&& (symbol.as_str().starts_with('/') || symbol.as_str().starts_with('\\')) | ||
{ | ||
span_lint_and_then( | ||
cx, | ||
JOIN_ABSOLUTE_PATHS, | ||
join_arg.span, | ||
"argument to `Path::join` starts with a path separator", | ||
|diag| { | ||
diag | ||
.note("joining a path starting with separator will replace the path instead") | ||
.help(r#"if this is unintentional, try removing the starting separator"#) | ||
.help(r#"if this is intentional, try creating a new Path instead"#); | ||
}, | ||
); | ||
} | ||
} |
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
6 changes: 3 additions & 3 deletions
6
tests/ui/join_absolute_path.fixed → tests/ui/join_absolute_paths.fixed
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
7 changes: 1 addition & 6 deletions
7
tests/ui/join_absolute_path.rs → tests/ui/join_absolute_paths.rs
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 |
---|---|---|
@@ -1,26 +1,21 @@ | ||
//@run-rustfix | ||
#![allow(unused)] | ||
#![warn(clippy::join_absolute_path)] | ||
#![warn(clippy::join_absolute_paths)] | ||
use std::path::Path; | ||
|
||
fn main() { | ||
// should be linted | ||
let path = Path::new("/bin"); | ||
path.join("/sh"); | ||
println!("{}", path.display()); | ||
|
||
//should be linted | ||
let path = Path::new("C:\\Users"); | ||
path.join("\\user"); | ||
println!("{}", path.display()); | ||
|
||
// should not be linted | ||
let path: &[&str] = &["/bin"]; | ||
path.join("/sh"); | ||
println!("{:?}", path); | ||
|
||
//should not be linted | ||
let 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,23 @@ | ||
error: argument to `Path::join` starts with a path separator | ||
--> $DIR/join_absolute_paths.rs:8:15 | ||
| | ||
LL | path.join("/sh"); | ||
| ^^^^^ | ||
| | ||
= note: joining a path starting with separator will replace the path instead | ||
= help: if this is unintentional, try removing the starting separator | ||
= help: if this is intentional, try creating a new Path instead | ||
= note: `-D clippy::join-absolute-paths` implied by `-D warnings` | ||
|
||
error: argument to `Path::join` starts with a path separator | ||
--> $DIR/join_absolute_paths.rs:12:15 | ||
| | ||
LL | path.join("//user"); | ||
| ^^^^^^^^ | ||
| | ||
= note: joining a path starting with separator will replace the path instead | ||
= help: if this is unintentional, try removing the starting separator | ||
= help: if this is intentional, try creating a new Path instead | ||
|
||
error: aborting due to 2 previous errors | ||
|