|
| 1 | +use clippy_utils::{ |
| 2 | + diagnostics::span_lint_and_then, get_parent_expr, is_from_proc_macro, match_def_path, path_res, paths::PATH_NEW, |
| 3 | + ty::is_type_diagnostic_item, |
| 4 | +}; |
| 5 | +use rustc_ast::LitKind; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::def_id::DefId; |
| 8 | +use rustc_hir::*; |
| 9 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 10 | +use rustc_middle::{lint::in_external_macro, ty}; |
| 11 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 12 | +use rustc_span::{sym, Symbol}; |
| 13 | + |
| 14 | +declare_clippy_lint! { |
| 15 | + /// ### What it does |
| 16 | + /// TODO please do soon |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// TODO please |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```rust |
| 23 | + /// // TODO |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```rust |
| 27 | + /// // TODO |
| 28 | + /// ``` |
| 29 | + #[clippy::version = "1.72.0"] |
| 30 | + pub BARE_DOS_DEVICE_NAMES, |
| 31 | + suspicious, |
| 32 | + "usage of paths that, on Windows, will implicitly refer to a DOS device" |
| 33 | +} |
| 34 | +declare_lint_pass!(BareDosDeviceNames => [BARE_DOS_DEVICE_NAMES]); |
| 35 | + |
| 36 | +impl<'tcx> LateLintPass<'tcx> for BareDosDeviceNames { |
| 37 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 38 | + if !in_external_macro(cx.sess(), expr.span) |
| 39 | + && let ExprKind::Lit(arg) = expr.kind |
| 40 | + && let LitKind::Str(str_sym, _) = arg.node |
| 41 | + && matches!( |
| 42 | + &*str_sym.as_str().to_ascii_lowercase(), |
| 43 | + "aux" |
| 44 | + | "con" |
| 45 | + | "conin$" |
| 46 | + // ^^^^^^ |
| 47 | + | "conout$" |
| 48 | + // ^^^^^^^ |
| 49 | + // TODO: Maybe these two can be an exception. |
| 50 | + // |
| 51 | + // Using `CONIN$` and `CONOUT$` is common enough in other languages that it may |
| 52 | + // trip up a couple newbies coming to rust. Besides, it's unlikely someone will |
| 53 | + // ever use `CONIN$` as a filename. |
| 54 | + | "com1" |
| 55 | + | "com2" |
| 56 | + | "com3" |
| 57 | + | "com4" |
| 58 | + | "com5" |
| 59 | + | "com6" |
| 60 | + | "com7" |
| 61 | + | "com8" |
| 62 | + | "com9" |
| 63 | + | "lpt1" |
| 64 | + | "lpt2" |
| 65 | + | "lpt3" |
| 66 | + | "lpt4" |
| 67 | + | "lpt5" |
| 68 | + | "lpt6" |
| 69 | + | "lpt7" |
| 70 | + | "lpt8" |
| 71 | + | "lpt9" |
| 72 | + | "nul" |
| 73 | + | "prn" |
| 74 | + ) |
| 75 | + && let Some(parent) = get_parent_expr(cx, expr) |
| 76 | + && (is_path_buf_from_or_path_new(cx, parent) || is_path_ty(cx, expr, parent)) |
| 77 | + && !is_from_proc_macro(cx, expr) |
| 78 | + { |
| 79 | + span_lint_and_then( |
| 80 | + cx, |
| 81 | + BARE_DOS_DEVICE_NAMES, |
| 82 | + expr.span, |
| 83 | + "this path refers to a DOS device", |
| 84 | + |diag| { |
| 85 | + // Suggest making current behavior explicit |
| 86 | + diag.span_suggestion_verbose( |
| 87 | + expr.span, |
| 88 | + "if this is intended, try", |
| 89 | + format!(r#""\\.\{str_sym}""#), |
| 90 | + Applicability::MaybeIncorrect, |
| 91 | + ); |
| 92 | + |
| 93 | + // Suggest making the code refer to a file or folder in the current directory |
| 94 | + diag.span_suggestion_verbose( |
| 95 | + expr.span, |
| 96 | + "if this was intended to point to a file or folder, try", |
| 97 | + format!("\"./{str_sym}\""), |
| 98 | + Applicability::MaybeIncorrect, |
| 99 | + ); |
| 100 | + } |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/// Gets whether the `Expr` is an argument to `Path::new` or `PathBuf::from`. The caller must |
| 107 | +/// provide the parent `Expr`, for performance's sake. |
| 108 | +/// |
| 109 | +/// TODO: We can likely refactor this like we did with `LINTED_TRAITS`. |
| 110 | +fn is_path_buf_from_or_path_new(cx: &LateContext<'_>, parent: &Expr<'_>) -> bool { |
| 111 | + if let ExprKind::Call(path, _) = parent.kind |
| 112 | + && let ExprKind::Path(qpath) = path.kind |
| 113 | + && let QPath::TypeRelative(ty, last_segment) = qpath |
| 114 | + && let Some(call_def_id) = path_res(cx, path).opt_def_id() |
| 115 | + && let Some(ty_def_id) = path_res(cx, ty).opt_def_id() |
| 116 | + && (match_def_path(cx, call_def_id, &PATH_NEW) |
| 117 | + // `PathBuf::from` is unfortunately tricky, as all we end up having for `match_def_path` |
| 118 | + // is `core::convert::From::from`, not `std::path::PathBuf::from`. Basically useless. |
| 119 | + || cx.tcx.is_diagnostic_item(sym::PathBuf, ty_def_id) && last_segment.ident.as_str() == "from") |
| 120 | + { |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + false |
| 125 | +} |
| 126 | + |
| 127 | +/// Gets the `DefId` and arguments of `expr`, if it's a `Call` or `MethodCall` |
| 128 | +fn get_def_id_and_args<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<(DefId, &'tcx [Expr<'tcx>])> { |
| 129 | + match expr.kind { |
| 130 | + ExprKind::Call(path, args) => Some((path_res(cx, path).opt_def_id()?, args)), |
| 131 | + ExprKind::MethodCall(_, _, args, _) => Some((cx.typeck_results().type_dependent_def_id(expr.hir_id)?, args)), |
| 132 | + _ => None, |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/// Given a `Ty`, returns whether it is likely a path type, like `Path` or `PathBuf`. Also returns |
| 137 | +/// true if it's `impl AsRef<Path>`, `T: AsRef<Path>`, etc. You get the idea. |
| 138 | +fn is_path_ty<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, parent: &'tcx Expr<'tcx>) -> bool { |
| 139 | + const LINTED_TRAITS: &[(Symbol, Symbol)] = &[ |
| 140 | + (sym::AsRef, sym::Path), |
| 141 | + (sym::Into, sym::PathBuf), |
| 142 | + (sym::Into, sym::Path), |
| 143 | + // TODO: Let's add more traits here. |
| 144 | + ]; |
| 145 | + |
| 146 | + let Some((callee, callee_args)) = get_def_id_and_args(cx, parent) else { |
| 147 | + return false; |
| 148 | + }; |
| 149 | + let Some(arg_index) = callee_args.iter().position(|arg| arg.hir_id == expr.hir_id) else { |
| 150 | + return false; |
| 151 | + }; |
| 152 | + let arg_ty = cx.tcx.fn_sig(callee).subst_identity().inputs().skip_binder()[arg_index].peel_refs(); |
| 153 | + |
| 154 | + // If we find `PathBuf` or `Path`, no need to check `impl <trait>` or `T`. |
| 155 | + if let Some(def) = arg_ty.ty_adt_def() |
| 156 | + && let def_id = def.did() |
| 157 | + && (cx.tcx.is_diagnostic_item(sym::PathBuf, def_id) || cx.tcx.is_diagnostic_item(sym::Path, def_id)) |
| 158 | + { |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + for predicate in cx |
| 163 | + .tcx |
| 164 | + .param_env(callee) |
| 165 | + .caller_bounds() |
| 166 | + .iter() |
| 167 | + .filter_map(|predicate| predicate.kind().no_bound_vars()) |
| 168 | + { |
| 169 | + if let ty::ClauseKind::Trait(trit) = predicate |
| 170 | + && trit.trait_ref.self_ty() == arg_ty |
| 171 | + // I believe `0` is always `Self`, so `T` or `impl <trait>` |
| 172 | + && let [_, subst] = trit.trait_ref.substs.as_slice() |
| 173 | + && let Some(as_ref_ty) = subst.as_type() |
| 174 | + { |
| 175 | + for (trait_sym, ty_sym) in LINTED_TRAITS { |
| 176 | + if cx.tcx.is_diagnostic_item(*trait_sym, trit.trait_ref.def_id) |
| 177 | + && is_type_diagnostic_item(cx, as_ref_ty, *ty_sym) |
| 178 | + { |
| 179 | + return true; |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + false |
| 186 | +} |
0 commit comments