2019-05-14 08:06:21 +00:00
|
|
|
use crate::utils::{match_type, paths, span_lint_and_sugg, walk_ptrs_ty};
|
2019-04-13 16:47:46 +00:00
|
|
|
use if_chain::if_chain;
|
2019-12-03 23:16:03 +00:00
|
|
|
use rustc::declare_lint_pass;
|
2019-04-13 16:47:46 +00:00
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc_errors::Applicability;
|
2019-12-03 23:16:03 +00:00
|
|
|
use rustc_session::declare_tool_lint;
|
2019-04-13 16:47:46 +00:00
|
|
|
use std::path::{Component, Path};
|
|
|
|
use syntax::ast::LitKind;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:*** Checks for [push](https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.push)
|
|
|
|
/// calls on `PathBuf` that can cause overwrites.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Calling `push` with a root path at the start can overwrite the
|
|
|
|
/// previous defined path.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```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"));
|
|
|
|
/// ```
|
|
|
|
pub PATH_BUF_PUSH_OVERWRITE,
|
2019-04-21 13:22:06 +00:00
|
|
|
nursery,
|
2019-04-13 16:47:46 +00:00
|
|
|
"calling `push` with file system root on `PathBuf` can overwrite it"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(PathBufPushOverwrite => [PATH_BUF_PUSH_OVERWRITE]);
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PathBufPushOverwrite {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
|
|
|
if_chain! {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::MethodCall(ref path, _, ref args) = expr.kind;
|
2019-05-17 21:53:54 +00:00
|
|
|
if path.ident.name == sym!(push);
|
2019-04-13 16:47:46 +00:00
|
|
|
if args.len() == 2;
|
2019-05-17 21:53:54 +00:00
|
|
|
if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(&args[0])), &paths::PATH_BUF);
|
2019-04-13 16:47:46 +00:00
|
|
|
if let Some(get_index_arg) = args.get(1);
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Lit(ref lit) = get_index_arg.kind;
|
2019-04-13 16:47:46 +00:00
|
|
|
if let LitKind::Str(ref path_lit, _) = lit.node;
|
2019-11-06 09:50:24 +00:00
|
|
|
if let pushed_path = Path::new(&*path_lit.as_str());
|
2019-04-13 16:47:46 +00: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,
|
|
|
|
"Calling `push` with '/' or '\\' (file system root) will overwrite the previous path definition",
|
|
|
|
"try",
|
|
|
|
format!("\"{}\"", pushed_path_lit.trim_start_matches(|c| c == '/' || c == '\\')),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|