|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::{snippet_opt, snippet_with_applicability}; |
| 3 | +use clippy_utils::ty::match_type; |
| 4 | +use clippy_utils::{match_def_path, paths}; |
| 5 | +use if_chain::if_chain; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{Expr, ExprKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// **What it does:** Checks for non-octal values used to set Unix file permissions. |
| 13 | + /// |
| 14 | + /// **Why is this bad?** They will be converted into octal, creating potentially |
| 15 | + /// unintended file permissions. |
| 16 | + /// |
| 17 | + /// **Known problems:** None. |
| 18 | + /// |
| 19 | + /// **Example:** |
| 20 | + /// |
| 21 | + /// ```rust,ignore |
| 22 | + /// use std::fs::OpenOptions; |
| 23 | + /// use std::os::unix::fs::OpenOptionsExt; |
| 24 | + /// |
| 25 | + /// let mut options = OpenOptions::new(); |
| 26 | + /// options.mode(644); |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```rust,ignore |
| 30 | + /// use std::fs::OpenOptions; |
| 31 | + /// use std::os::unix::fs::OpenOptionsExt; |
| 32 | + /// |
| 33 | + /// let mut options = OpenOptions::new(); |
| 34 | + /// options.mode(0o644); |
| 35 | + /// ``` |
| 36 | + pub NON_OCTAL_UNIX_PERMISSIONS, |
| 37 | + correctness, |
| 38 | + "use of non-octal value to set unix file permissions, which will be translated into octal" |
| 39 | +} |
| 40 | + |
| 41 | +declare_lint_pass!(NonOctalUnixPermissions => [NON_OCTAL_UNIX_PERMISSIONS]); |
| 42 | + |
| 43 | +impl LateLintPass<'_> for NonOctalUnixPermissions { |
| 44 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 45 | + match &expr.kind { |
| 46 | + ExprKind::MethodCall(path, _, [func, param], _) => { |
| 47 | + let obj_ty = cx.typeck_results().expr_ty(&func).peel_refs(); |
| 48 | + |
| 49 | + if_chain! { |
| 50 | + if (path.ident.name == sym!(mode) |
| 51 | + && (match_type(cx, obj_ty, &paths::OPEN_OPTIONS) |
| 52 | + || match_type(cx, obj_ty, &paths::DIR_BUILDER))) |
| 53 | + || (path.ident.name == sym!(set_mode) && match_type(cx, obj_ty, &paths::PERMISSIONS)); |
| 54 | + if let ExprKind::Lit(_) = param.kind; |
| 55 | + |
| 56 | + then { |
| 57 | + let snip = match snippet_opt(cx, param.span) { |
| 58 | + Some(s) => s, |
| 59 | + _ => return, |
| 60 | + }; |
| 61 | + |
| 62 | + if !snip.starts_with("0o") { |
| 63 | + show_error(cx, param); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + }, |
| 68 | + ExprKind::Call(ref func, [param]) => { |
| 69 | + if_chain! { |
| 70 | + if let ExprKind::Path(ref path) = func.kind; |
| 71 | + if let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id(); |
| 72 | + if match_def_path(cx, def_id, &paths::PERMISSIONS_FROM_MODE); |
| 73 | + if let ExprKind::Lit(_) = param.kind; |
| 74 | + |
| 75 | + then { |
| 76 | + let snip = match snippet_opt(cx, param.span) { |
| 77 | + Some(s) => s, |
| 78 | + _ => return, |
| 79 | + }; |
| 80 | + |
| 81 | + if !snip.starts_with("0o") { |
| 82 | + show_error(cx, param); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + }, |
| 87 | + _ => {}, |
| 88 | + }; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn show_error(cx: &LateContext<'_>, param: &Expr<'_>) { |
| 93 | + let mut applicability = Applicability::MachineApplicable; |
| 94 | + span_lint_and_sugg( |
| 95 | + cx, |
| 96 | + NON_OCTAL_UNIX_PERMISSIONS, |
| 97 | + param.span, |
| 98 | + "using a non-octal value to set unix file permissions", |
| 99 | + "consider using an octal literal instead", |
| 100 | + format!( |
| 101 | + "0o{}", |
| 102 | + snippet_with_applicability(cx, param.span, "0o..", &mut applicability,), |
| 103 | + ), |
| 104 | + applicability, |
| 105 | + ); |
| 106 | +} |
0 commit comments