]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/path_buf_push_overwrite.rs
Rustup to rust-lang/rust#66878
[rust.git] / clippy_lints / src / path_buf_push_overwrite.rs
1 use crate::utils::{match_type, paths, span_lint_and_sugg, walk_ptrs_ty};
2 use if_chain::if_chain;
3 use rustc::declare_lint_pass;
4 use rustc::hir::*;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc_errors::Applicability;
7 use rustc_session::declare_tool_lint;
8 use std::path::{Component, Path};
9 use syntax::ast::LitKind;
10
11 declare_clippy_lint! {
12     /// **What it does:*** Checks for [push](https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.push)
13     /// calls on `PathBuf` that can cause overwrites.
14     ///
15     /// **Why is this bad?** Calling `push` with a root path at the start can overwrite the
16     /// previous defined path.
17     ///
18     /// **Known problems:** None.
19     ///
20     /// **Example:**
21     /// ```rust
22     /// use std::path::PathBuf;
23     ///
24     /// let mut x = PathBuf::from("/foo");
25     /// x.push("/bar");
26     /// assert_eq!(x, PathBuf::from("/bar"));
27     /// ```
28     /// Could be written:
29     ///
30     /// ```rust
31     /// use std::path::PathBuf;
32     ///
33     /// let mut x = PathBuf::from("/foo");
34     /// x.push("bar");
35     /// assert_eq!(x, PathBuf::from("/foo/bar"));
36     /// ```
37     pub PATH_BUF_PUSH_OVERWRITE,
38     nursery,
39     "calling `push` with file system root on `PathBuf` can overwrite it"
40 }
41
42 declare_lint_pass!(PathBufPushOverwrite => [PATH_BUF_PUSH_OVERWRITE]);
43
44 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PathBufPushOverwrite {
45     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
46         if_chain! {
47             if let ExprKind::MethodCall(ref path, _, ref args) = expr.kind;
48             if path.ident.name == sym!(push);
49             if args.len() == 2;
50             if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(&args[0])), &paths::PATH_BUF);
51             if let Some(get_index_arg) = args.get(1);
52             if let ExprKind::Lit(ref lit) = get_index_arg.kind;
53             if let LitKind::Str(ref path_lit, _) = lit.node;
54             if let pushed_path = Path::new(&*path_lit.as_str());
55             if let Some(pushed_path_lit) = pushed_path.to_str();
56             if pushed_path.has_root();
57             if let Some(root) = pushed_path.components().next();
58             if root == Component::RootDir;
59             then {
60                 span_lint_and_sugg(
61                     cx,
62                     PATH_BUF_PUSH_OVERWRITE,
63                     lit.span,
64                     "Calling `push` with '/' or '\\' (file system root) will overwrite the previous path definition",
65                     "try",
66                     format!("\"{}\"", pushed_path_lit.trim_start_matches(|c| c == '/' || c == '\\')),
67                     Applicability::MachineApplicable,
68                 );
69             }
70         }
71     }
72 }