|
| 1 | +#![allow(clippy::similar_names)] |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 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 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks that the caller enables the target features that the callee requires |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// Not enabling target features can cause UB and limits optimization opportunities. |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// ```no_run |
| 19 | + /// #[target_feature(enable = "avx2")] |
| 20 | + /// unsafe fn f() -> u32 { |
| 21 | + /// 0 |
| 22 | + /// } |
| 23 | + /// |
| 24 | + /// fn g() { |
| 25 | + /// unsafe { f() }; |
| 26 | + /// // g does not enable the target features f requires |
| 27 | + /// } |
| 28 | + /// ``` |
| 29 | + #[clippy::version = "1.82.0"] |
| 30 | + pub CALL_MISSING_TARGET_FEATURE, |
| 31 | + suspicious, |
| 32 | + "call requires target features that the surrounding function does not enable" |
| 33 | +} |
| 34 | + |
| 35 | +declare_lint_pass!(CallMissingTargetFeature => [CALL_MISSING_TARGET_FEATURE]); |
| 36 | + |
| 37 | +impl<'tcx> LateLintPass<'tcx> for CallMissingTargetFeature { |
| 38 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'tcx>) { |
| 39 | + let Some(callee_def_id) = callee_def_id(cx, expr) else { |
| 40 | + return; |
| 41 | + }; |
| 42 | + let callee_target_features = &cx.tcx.codegen_fn_attrs(callee_def_id).target_features; |
| 43 | + |
| 44 | + if callee_target_features.is_empty() { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + let Some(caller_body_id) = cx.enclosing_body else { |
| 49 | + return; |
| 50 | + }; |
| 51 | + let caller_def_id = cx.tcx.hir().body_owner_def_id(caller_body_id); |
| 52 | + let caller_target_features = &cx.tcx.codegen_fn_attrs(caller_def_id).target_features; |
| 53 | + |
| 54 | + if in_external_macro(cx.tcx.sess, expr.span) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + // target features can imply other target features (e.g. avx2 implies sse4.2). We can safely skip |
| 59 | + // implied target features and only warn for the more general missing target feature. |
| 60 | + let missing: Vec<_> = callee_target_features |
| 61 | + .iter() |
| 62 | + .filter_map(|target_feature| { |
| 63 | + if target_feature.implied || caller_target_features.iter().any(|tf| tf.name == target_feature.name) { |
| 64 | + None |
| 65 | + } else { |
| 66 | + Some(target_feature.name.as_str()) |
| 67 | + } |
| 68 | + }) |
| 69 | + .collect(); |
| 70 | + |
| 71 | + if missing.is_empty() { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + let attr = format!("#[target_feature(enable = \"{}\")]", missing.join(",")); |
| 76 | + |
| 77 | + span_lint_and_then( |
| 78 | + cx, |
| 79 | + CALL_MISSING_TARGET_FEATURE, |
| 80 | + expr.span, |
| 81 | + "this call requires target features that the surrounding function does not enable", |
| 82 | + |diag| { |
| 83 | + diag.span_label( |
| 84 | + expr.span, |
| 85 | + "this function call requires target features to be enabled".to_string(), |
| 86 | + ); |
| 87 | + |
| 88 | + let fn_sig = cx.tcx.fn_sig(caller_def_id).skip_binder(); |
| 89 | + |
| 90 | + let mut suggestions = Vec::with_capacity(2); |
| 91 | + |
| 92 | + let hir::Node::Item(caller_item) = cx.tcx.hir_node_by_def_id(caller_def_id) else { |
| 93 | + return; |
| 94 | + }; |
| 95 | + |
| 96 | + let Some(indent) = clippy_utils::source::snippet_indent(cx, caller_item.span) else { |
| 97 | + return; |
| 98 | + }; |
| 99 | + |
| 100 | + let lo_span = caller_item.span.with_hi(caller_item.span.lo()); |
| 101 | + |
| 102 | + if let hir::Safety::Safe = fn_sig.safety() { |
| 103 | + if caller_item.vis_span.is_empty() { |
| 104 | + suggestions.push((lo_span, format!("{attr}\n{indent}unsafe "))); |
| 105 | + } else { |
| 106 | + suggestions.push((lo_span, format!("{attr}\n{indent}"))); |
| 107 | + suggestions.push((caller_item.vis_span.shrink_to_hi(), " unsafe".to_string())); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + diag.multipart_suggestion_verbose( |
| 112 | + "add the missing target features to the surrounding function", |
| 113 | + suggestions, |
| 114 | + rustc_errors::Applicability::MaybeIncorrect, |
| 115 | + ); |
| 116 | + }, |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +fn callee_def_id(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<DefId> { |
| 122 | + match expr.kind { |
| 123 | + hir::ExprKind::Call(path, _) => { |
| 124 | + if let hir::ExprKind::Path(ref qpath) = path.kind |
| 125 | + && let Res::Def(_, did) = cx.qpath_res(qpath, path.hir_id) |
| 126 | + { |
| 127 | + Some(did) |
| 128 | + } else { |
| 129 | + None |
| 130 | + } |
| 131 | + }, |
| 132 | + hir::ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id), |
| 133 | + _ => None, |
| 134 | + } |
| 135 | +} |
0 commit comments