2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2019-04-13 11:47:46 -05:00
|
|
|
use if_chain::if_chain;
|
2020-02-29 21:23:33 -06:00
|
|
|
use rustc_ast::ast::LitKind;
|
2019-04-13 11:47:46 -05:00
|
|
|
use rustc_errors::Applicability;
|
2020-02-21 02:39:38 -06:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_span::symbol::sym;
|
2019-04-13 11:47:46 -05:00
|
|
|
use std::path::{Component, Path};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
///* Checks for [push](https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.push)
|
2019-04-13 11:47:46 -05:00
|
|
|
/// calls on `PathBuf` that can cause overwrites.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Calling `push` with a root path at the start can overwrite the
|
2019-04-13 11:47:46 -05:00
|
|
|
/// previous defined path.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-04-13 11:47:46 -05:00
|
|
|
/// ```rust
|
|
|
|
/// use std::path::PathBuf;
|
|
|
|
///
|
|
|
|
/// let mut x = PathBuf::from("/foo");
|
|
|
|
/// x.push("/bar");
|
|
|
|
/// assert_eq!(x, PathBuf::from("/bar"));
|
|
|
|
/// ```
|
|
|
|
/// Could be written:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::path::PathBuf;
|
|
|
|
///
|
|
|
|
/// let mut x = PathBuf::from("/foo");
|
|
|
|
/// x.push("bar");
|
|
|
|
/// assert_eq!(x, PathBuf::from("/foo/bar"));
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.36.0"]
|
2019-04-13 11:47:46 -05:00
|
|
|
pub PATH_BUF_PUSH_OVERWRITE,
|
2019-04-21 08:22:06 -05:00
|
|
|
nursery,
|
2019-04-13 11:47:46 -05:00
|
|
|
"calling `push` with file system root on `PathBuf` can overwrite it"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(PathBufPushOverwrite => [PATH_BUF_PUSH_OVERWRITE]);
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for PathBufPushOverwrite {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2019-04-13 11:47:46 -05:00
|
|
|
if_chain! {
|
2021-12-01 11:17:50 -06:00
|
|
|
if let ExprKind::MethodCall(path, args, _) = expr.kind;
|
2019-05-17 16:53:54 -05:00
|
|
|
if path.ident.name == sym!(push);
|
2019-04-13 11:47:46 -05:00
|
|
|
if args.len() == 2;
|
2021-03-12 08:30:50 -06:00
|
|
|
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), sym::PathBuf);
|
2019-04-13 11:47:46 -05:00
|
|
|
if let Some(get_index_arg) = args.get(1);
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Lit(ref lit) = get_index_arg.kind;
|
2019-04-13 11:47:46 -05:00
|
|
|
if let LitKind::Str(ref path_lit, _) = lit.node;
|
2021-12-14 21:39:23 -06:00
|
|
|
if let pushed_path = Path::new(path_lit.as_str());
|
2019-04-13 11:47:46 -05:00
|
|
|
if let Some(pushed_path_lit) = pushed_path.to_str();
|
|
|
|
if pushed_path.has_root();
|
|
|
|
if let Some(root) = pushed_path.components().next();
|
|
|
|
if root == Component::RootDir;
|
|
|
|
then {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
PATH_BUF_PUSH_OVERWRITE,
|
|
|
|
lit.span,
|
2020-08-11 08:43:21 -05:00
|
|
|
"calling `push` with '/' or '\\' (file system root) will overwrite the previous path definition",
|
2019-04-13 11:47:46 -05:00
|
|
|
"try",
|
|
|
|
format!("\"{}\"", pushed_path_lit.trim_start_matches(|c| c == '/' || c == '\\')),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|