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