|
| 1 | +use rustc_ast::{BinOpKind, UnOp}; |
| 2 | +use rustc_hir::{self as hir, Expr, ExprKind, HirIdSet, LangItem, QPath, Stmt, StmtKind}; |
| 3 | +use rustc_macros::{LintDiagnostic, Subdiagnostic}; |
| 4 | +use rustc_middle::ty; |
| 5 | +use rustc_session::lint::FutureIncompatibilityReason; |
| 6 | +use rustc_session::{declare_lint, impl_lint_pass}; |
| 7 | +use rustc_span::{Span, sym}; |
| 8 | + |
| 9 | +use crate::{LateContext, LateLintPass, LintContext}; |
| 10 | + |
| 11 | +declare_lint! { |
| 12 | + /// The `cargo_cfg_target_family_multivalued` lint detects single-valued comparisons of [the |
| 13 | + /// `CARGO_CFG_TARGET_FAMILY`][CARGO_CFG_TARGET_FAMILY] environment variable. |
| 14 | + /// |
| 15 | + /// This variable is set by Cargo in build scripts. |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// |
| 19 | + /// ```rust,no_run |
| 20 | + /// // build.rs |
| 21 | + /// fn main() { |
| 22 | + /// let target_family = std::env::var("CARGO_CFG_TARGET_FAMILY").unwrap(); |
| 23 | + /// |
| 24 | + /// if target_family == "unix" { |
| 25 | + /// // Do something specific to Unix platforms |
| 26 | + /// } |
| 27 | + /// } |
| 28 | + /// ``` |
| 29 | + /// |
| 30 | + /// {{produces}} |
| 31 | + /// |
| 32 | + /// ### Explanation |
| 33 | + /// |
| 34 | + /// `CARGO_CFG_TARGET_FAMILY` is taken from [the `target_family` cfg][cfg-target_family], which |
| 35 | + /// may be set multiple times. This means that `CARGO_CFG_TARGET_FAMILY` can consist of multiple |
| 36 | + /// values, separated by commas. Comparing against a single value is thus not cross-platform. |
| 37 | + /// |
| 38 | + /// Note that most targets currently only have a single `target_family`, so oftentimes you |
| 39 | + /// wouldn't hit this. This is a [future-incompatible] lint, since the compiler may at some |
| 40 | + /// point introduce further target families for existing targets, and then a simple comparison |
| 41 | + /// would no longer work. |
| 42 | + /// |
| 43 | + /// [CARGO_CFG_TARGET_FAMILY]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#:~:text=CARGO_CFG_TARGET_FAMILY |
| 44 | + /// [cfg-target_family]: https://doc.rust-lang.org/reference/conditional-compilation.html#target_family |
| 45 | + CARGO_CFG_TARGET_FAMILY_MULTIVALUED, |
| 46 | + Warn, |
| 47 | + "comparing `CARGO_CFG_TARGET_FAMILY` env var with a single value", |
| 48 | + @future_incompatible = FutureIncompatibleInfo { |
| 49 | + reason: FutureIncompatibilityReason::FutureReleaseSemanticsChange, |
| 50 | + reference: "issue #100343 <https://github.com/rust-lang/rust/issues/100343>", |
| 51 | + explain_reason: false, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Default)] |
| 56 | +pub(crate) struct CargoCfgTargetFamilyMultivalued { |
| 57 | + /// A side table of locals that are initialized from |
| 58 | + /// `std::env::var("CARGO_CFG_TARGET_FAMILY")` or similar. |
| 59 | + target_family_locals: HirIdSet, |
| 60 | +} |
| 61 | + |
| 62 | +impl_lint_pass!(CargoCfgTargetFamilyMultivalued => [CARGO_CFG_TARGET_FAMILY_MULTIVALUED]); |
| 63 | + |
| 64 | +#[derive(LintDiagnostic)] |
| 65 | +#[diag(lint_cargo_cfg_target_family_multivalued_comparison)] |
| 66 | +#[note] |
| 67 | +struct SingleValuedComparison { |
| 68 | + #[subdiagnostic] |
| 69 | + sugg: Option<ReplaceWithSplitAny>, |
| 70 | +} |
| 71 | + |
| 72 | +#[derive(Subdiagnostic)] |
| 73 | +#[multipart_suggestion(lint_suggestion, applicability = "machine-applicable")] |
| 74 | +struct ReplaceWithSplitAny { |
| 75 | + #[suggestion_part(code = "!")] |
| 76 | + negate: Option<Span>, |
| 77 | + #[suggestion_part(code = ".split(',').any(|x| x == ")] |
| 78 | + op: Span, |
| 79 | + #[suggestion_part(code = ")")] |
| 80 | + end: Span, |
| 81 | +} |
| 82 | + |
| 83 | +#[derive(LintDiagnostic)] |
| 84 | +#[diag(lint_cargo_cfg_target_family_multivalued_match)] |
| 85 | +#[note] |
| 86 | +#[note(lint_suggestion)] |
| 87 | +struct SingleValuedMatch; |
| 88 | + |
| 89 | +// NOTE: We choose not to do a check for when in a build script, like: |
| 90 | +// matches!(&sess.opts.crate_name, Some(crate_name) if crate_name == "build_script_build") |
| 91 | +// Since we might be building a library that is used as a build script dependency (`cc-rs` etc). |
| 92 | +impl<'tcx> LateLintPass<'tcx> for CargoCfgTargetFamilyMultivalued { |
| 93 | + fn check_stmt(&mut self, _cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'tcx>) { |
| 94 | + // Find locals that are initialized from `CARGO_CFG_TARGET_FAMILY`, and save them for later |
| 95 | + // checking. |
| 96 | + if let StmtKind::Let(stmt) = &stmt.kind { |
| 97 | + if let Some(init) = stmt.init { |
| 98 | + if self.accesses_target_family_env(init) { |
| 99 | + stmt.pat.each_binding(|_, hir_id, _, _| { |
| 100 | + self.target_family_locals.insert(hir_id); |
| 101 | + }); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 108 | + // Check expressions that do single-valued comparisons. |
| 109 | + match &expr.kind { |
| 110 | + ExprKind::Binary(op, a, b) if matches!(op.node, BinOpKind::Eq | BinOpKind::Ne) => { |
| 111 | + if self.accesses_target_family_env(a) { |
| 112 | + // If this is a &str or String, we can confidently give a `.split()` suggestion. |
| 113 | + let a_ty = cx.typeck_results().expr_ty(a); |
| 114 | + let is_str = matches!( |
| 115 | + a_ty.kind(), |
| 116 | + ty::Ref(_, r, _) if r.is_str(), |
| 117 | + ) || matches!( |
| 118 | + a_ty.ty_adt_def(), |
| 119 | + Some(ty_def) if cx.tcx.is_lang_item(ty_def.did(), LangItem::String), |
| 120 | + ); |
| 121 | + let sugg = is_str.then(|| ReplaceWithSplitAny { |
| 122 | + negate: (op.node == BinOpKind::Ne).then(|| expr.span.shrink_to_lo()), |
| 123 | + op: a.span.between(b.span), |
| 124 | + end: b.span.shrink_to_hi(), |
| 125 | + }); |
| 126 | + |
| 127 | + cx.emit_span_lint( |
| 128 | + CARGO_CFG_TARGET_FAMILY_MULTIVALUED, |
| 129 | + expr.span, |
| 130 | + SingleValuedComparison { sugg }, |
| 131 | + ); |
| 132 | + } else if self.accesses_target_family_env(b) { |
| 133 | + cx.emit_span_lint( |
| 134 | + CARGO_CFG_TARGET_FAMILY_MULTIVALUED, |
| 135 | + expr.span, |
| 136 | + // Unsure how to emit a suggestion when we need to reorder `a` and `b`. |
| 137 | + SingleValuedComparison { sugg: None }, |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | + ExprKind::Match(expr, _, _) if self.accesses_target_family_env(expr) => { |
| 142 | + cx.emit_span_lint( |
| 143 | + CARGO_CFG_TARGET_FAMILY_MULTIVALUED, |
| 144 | + expr.span, |
| 145 | + SingleValuedMatch, |
| 146 | + ); |
| 147 | + } |
| 148 | + // We don't handle method calls like `PartialEq::eq`, that's probably fine though, |
| 149 | + // those are uncommon in real-world code. |
| 150 | + _ => {} |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl CargoCfgTargetFamilyMultivalued { |
| 156 | + /// Check if an expression is likely derived from the `CARGO_CFG_TARGET_FAMILY` env var. |
| 157 | + fn accesses_target_family_env(&self, expr: &Expr<'_>) -> bool { |
| 158 | + match &expr.kind { |
| 159 | + // A call to `std::env::var[_os]("CARGO_CFG_TARGET_FAMILY")`. |
| 160 | + // |
| 161 | + // NOTE: This actually matches all functions that take as a single value |
| 162 | + // `"CARGO_CFG_TARGET_FAMILY"`. We could restrict this by matching only functions that |
| 163 | + // match `"std::env::var"` or `"std::env::var_os"` by doing something like: |
| 164 | + // |
| 165 | + // && let Expr { kind: ExprKind::Path(QPath::Resolved(_, path)), .. } = func |
| 166 | + // && let Some(fn_def_id) = path.res.opt_def_id() |
| 167 | + // && cx.tcx.is_diagnostic_item(sym::std_env_var, fn_def_id) |
| 168 | + // |
| 169 | + // But users often define wrapper functions around these, and so we wouldn't catch it |
| 170 | + // when they do. |
| 171 | + // |
| 172 | + // This is probably fine, `"CARGO_CFG_TARGET_FAMILY"` is unique enough of a name that |
| 173 | + // it's unlikely that people will be using it for anything else. |
| 174 | + ExprKind::Call(_, [arg]) |
| 175 | + if let ExprKind::Lit(lit) = &arg.kind |
| 176 | + && lit.node.str() == Some(sym::cargo_cfg_target_family) => |
| 177 | + { |
| 178 | + true |
| 179 | + } |
| 180 | + // On local variables, try to see if it was initialized from target family earlier. |
| 181 | + ExprKind::Path(QPath::Resolved(_, path)) |
| 182 | + if let hir::def::Res::Local(local_hir_id) = &path.res => |
| 183 | + { |
| 184 | + self.target_family_locals.contains(local_hir_id) |
| 185 | + } |
| 186 | + // Recurse through references and dereferences. |
| 187 | + ExprKind::AddrOf(_, _, expr) | ExprKind::Unary(UnOp::Deref, expr) => { |
| 188 | + self.accesses_target_family_env(expr) |
| 189 | + } |
| 190 | + // Recurse on every method call to allow `.unwrap()`, `.as_deref()` and similar. |
| 191 | + // |
| 192 | + // NOTE: We could consider only recursing on specific `Option`/`Result` methods, but the |
| 193 | + // full list of the ones we'd want becomes unwieldy pretty quickly. |
| 194 | + ExprKind::MethodCall(_, receiver, _, _) => self.accesses_target_family_env(receiver), |
| 195 | + _ => false, |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments