|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::sugg::DiagExt; |
| 3 | +use rustc_hir as hir; |
| 4 | +use rustc_hir::def::Res; |
| 5 | +use rustc_hir::def_id::DefId; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_middle::lint::in_external_macro; |
| 8 | +use rustc_session::declare_lint_pass; |
| 9 | +use rustc_span::{sym, Symbol}; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// ### What it does |
| 13 | + /// Checks that the caller enables the target features that the callee requires |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// Not enabling target features can cause UB and limits optimization opportunities. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// ```no_run |
| 20 | + /// #[target_feature(enable = "avx2")] |
| 21 | + /// unsafe fn f() -> u32 { |
| 22 | + /// 0 |
| 23 | + /// } |
| 24 | + /// |
| 25 | + /// fn g() { |
| 26 | + /// unsafe { f() }; |
| 27 | + /// // g does not enable the target features f requires |
| 28 | + /// } |
| 29 | + /// ``` |
| 30 | + #[clippy::version = "CURRENT_CLIPPY_VERSION"] |
| 31 | + pub CALL_MISSING_TARGET_FEATURE, |
| 32 | + suspicious, |
| 33 | + "call requires target features that the surrounding function does not enable" |
| 34 | +} |
| 35 | + |
| 36 | +declare_lint_pass!(CallMissingTargetFeature => [CALL_MISSING_TARGET_FEATURE]); |
| 37 | + |
| 38 | +impl<'tcx> LateLintPass<'tcx> for CallMissingTargetFeature { |
| 39 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'tcx>) { |
| 40 | + if !in_external_macro(cx.tcx.sess, expr.span) { |
| 41 | + let Some(caller_def_id) = caller_def_id(cx, expr) else { |
| 42 | + return; |
| 43 | + }; |
| 44 | + let Some(callee_def_id) = callee_def_id(cx, expr) else { |
| 45 | + return; |
| 46 | + }; |
| 47 | + |
| 48 | + let caller_target_features = def_id_target_features(cx, caller_def_id); |
| 49 | + let callee_target_features = def_id_target_features(cx, callee_def_id); |
| 50 | + |
| 51 | + let missing: Vec<_> = callee_target_features |
| 52 | + .iter() |
| 53 | + .filter(|target_feature| !caller_target_features.contains(target_feature)) |
| 54 | + .map(|target_feature| target_feature.as_str()) |
| 55 | + .collect(); |
| 56 | + |
| 57 | + if missing.is_empty() { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + let hint = format!("#[target_feature(enable = \"{}\")]", missing.join(", ")); |
| 62 | + |
| 63 | + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] |
| 64 | + span_lint_and_then( |
| 65 | + cx, |
| 66 | + CALL_MISSING_TARGET_FEATURE, |
| 67 | + expr.span, |
| 68 | + "this call requires target features that the surrounding function does not enable", |
| 69 | + |diag| { |
| 70 | + diag.span_label( |
| 71 | + expr.span, |
| 72 | + format!("this function call requires target features to be enabled"), |
| 73 | + ); |
| 74 | + |
| 75 | + if let Some(caller_item) = caller_item(cx, expr) { |
| 76 | + diag.suggest_item_with_attr( |
| 77 | + cx, |
| 78 | + caller_item.span, |
| 79 | + "add the missing target features to the surrounding function", |
| 80 | + hint.as_str(), |
| 81 | + rustc_errors::Applicability::MaybeIncorrect, |
| 82 | + ); |
| 83 | + } |
| 84 | + }, |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +fn callee_def_id(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<DefId> { |
| 91 | + match expr.kind { |
| 92 | + hir::ExprKind::Call(path, _) => { |
| 93 | + if let hir::ExprKind::Path(ref qpath) = path.kind |
| 94 | + && let Res::Def(_, did) = cx.qpath_res(qpath, path.hir_id) |
| 95 | + { |
| 96 | + Some(did) |
| 97 | + } else { |
| 98 | + None |
| 99 | + } |
| 100 | + }, |
| 101 | + hir::ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id), |
| 102 | + _ => None, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +fn caller_def_id<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'tcx>) -> Option<DefId> { |
| 107 | + let item = caller_item(cx, expr)?; |
| 108 | + Some(item.owner_id.to_def_id()) |
| 109 | +} |
| 110 | + |
| 111 | +fn caller_item<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'tcx>) -> Option<&'tcx hir::Item<'tcx>> { |
| 112 | + for (_hir_id, node) in cx.tcx.hir().parent_iter(expr.hir_id) { |
| 113 | + if let hir::Node::Item( |
| 114 | + item @ hir::Item { |
| 115 | + kind: hir::ItemKind::Fn(..), |
| 116 | + .. |
| 117 | + }, |
| 118 | + ) = node |
| 119 | + { |
| 120 | + return Some(item); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + None |
| 125 | +} |
| 126 | + |
| 127 | +// return the target features that the called function depends on |
| 128 | +fn def_id_target_features(cx: &LateContext<'_>, did: DefId) -> Vec<Symbol> { |
| 129 | + let mut added_target_features = Vec::new(); |
| 130 | + |
| 131 | + for attr in cx.tcx.get_attrs(did, sym::target_feature) { |
| 132 | + let Some(list) = attr.meta_item_list() else { |
| 133 | + return vec![]; |
| 134 | + }; |
| 135 | + |
| 136 | + for item in list { |
| 137 | + // Only `enable = ...` is accepted in the meta-item list. |
| 138 | + if !item.has_name(sym::enable) { |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + // Must be of the form `enable = "..."` (a string). |
| 143 | + let Some(value) = item.value_str() else { |
| 144 | + continue; |
| 145 | + }; |
| 146 | + |
| 147 | + added_target_features.extend(value.as_str().split(',').map(|feature| Symbol::intern(feature))); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + added_target_features |
| 152 | +} |
0 commit comments