-
Notifications
You must be signed in to change notification settings - Fork 1.7k
new lint: add call_missing_target_feature
lint
#13240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
folkertdev
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
folkertdev:call-missing-target-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#![allow(clippy::similar_names)] | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use rustc_hir as hir; | ||
use rustc_hir::def::Res; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks that the caller enables the target features that the callee requires | ||
/// | ||
/// ### Why is this bad? | ||
/// Not enabling target features can cause UB and limits optimization opportunities. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// #[target_feature(enable = "avx2")] | ||
/// unsafe fn f() -> u32 { | ||
/// 0 | ||
/// } | ||
/// | ||
/// fn g() { | ||
/// unsafe { f() }; | ||
/// // g does not enable the target features f requires | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.82.0"] | ||
pub CALL_MISSING_TARGET_FEATURE, | ||
suspicious, | ||
"call requires target features that the surrounding function does not enable" | ||
} | ||
|
||
declare_lint_pass!(CallMissingTargetFeature => [CALL_MISSING_TARGET_FEATURE]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for CallMissingTargetFeature { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'tcx>) { | ||
let Some(callee_def_id) = callee_def_id(cx, expr) else { | ||
return; | ||
}; | ||
let callee_target_features = &cx.tcx.codegen_fn_attrs(callee_def_id).target_features; | ||
|
||
if callee_target_features.is_empty() { | ||
return; | ||
} | ||
|
||
let Some(caller_body_id) = cx.enclosing_body else { | ||
return; | ||
}; | ||
let caller_def_id = cx.tcx.hir().body_owner_def_id(caller_body_id); | ||
let caller_target_features = &cx.tcx.codegen_fn_attrs(caller_def_id).target_features; | ||
|
||
if in_external_macro(cx.tcx.sess, expr.span) { | ||
return; | ||
} | ||
|
||
// target features can imply other target features (e.g. avx2 implies sse4.2). We can safely skip | ||
// implied target features and only warn for the more general missing target feature. | ||
let missing: Vec<_> = callee_target_features | ||
.iter() | ||
.filter_map(|target_feature| { | ||
if target_feature.implied || caller_target_features.iter().any(|tf| tf.name == target_feature.name) { | ||
None | ||
} else { | ||
Some(target_feature.name.as_str()) | ||
} | ||
}) | ||
.collect(); | ||
|
||
if missing.is_empty() { | ||
return; | ||
} | ||
|
||
let attr = format!("#[target_feature(enable = \"{}\")]", missing.join(",")); | ||
|
||
span_lint_and_then( | ||
cx, | ||
CALL_MISSING_TARGET_FEATURE, | ||
expr.span, | ||
"this call requires target features that the surrounding function does not enable", | ||
|diag| { | ||
diag.span_label( | ||
expr.span, | ||
"this function call requires target features to be enabled".to_string(), | ||
); | ||
|
||
let fn_sig = cx.tcx.fn_sig(caller_def_id).skip_binder(); | ||
|
||
let mut suggestions = Vec::with_capacity(2); | ||
|
||
let hir::Node::Item(caller_item) = cx.tcx.hir_node_by_def_id(caller_def_id) else { | ||
return; | ||
}; | ||
|
||
let Some(indent) = clippy_utils::source::snippet_indent(cx, caller_item.span) else { | ||
return; | ||
}; | ||
|
||
let lo_span = caller_item.span.with_hi(caller_item.span.lo()); | ||
|
||
match fn_sig.safety() { | ||
hir::Safety::Safe => { | ||
// the `target_feature` attribute can only be applied to unsafe functions | ||
if caller_item.vis_span.is_empty() { | ||
suggestions.push((lo_span, format!("{attr}\n{indent}unsafe "))); | ||
} else { | ||
suggestions.push((lo_span, format!("{attr}\n{indent}"))); | ||
suggestions.push((caller_item.vis_span.shrink_to_hi(), " unsafe".to_string())); | ||
} | ||
}, | ||
hir::Safety::Unsafe => { | ||
suggestions.push((lo_span, format!("{attr}\n{indent}"))); | ||
}, | ||
} | ||
|
||
diag.multipart_suggestion_verbose( | ||
"add the missing target features to the surrounding function", | ||
suggestions, | ||
rustc_errors::Applicability::MaybeIncorrect, | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
fn callee_def_id(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<DefId> { | ||
match expr.kind { | ||
hir::ExprKind::Call(path, _) => { | ||
if let hir::ExprKind::Path(ref qpath) = path.kind | ||
&& let Res::Def(_, did) = cx.qpath_res(qpath, path.hir_id) | ||
{ | ||
Some(did) | ||
} else { | ||
None | ||
} | ||
}, | ||
hir::ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id), | ||
_ => None, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//@only-target: x86_64 | ||
#![allow(clippy::missing_safety_doc)] | ||
|
||
#[target_feature(enable = "avx2")] | ||
pub unsafe fn test_f() { | ||
unsafe { f() }; | ||
//~^ ERROR: this call requires target features | ||
} | ||
|
||
#[target_feature(enable = "avx2,pclmulqdq")] | ||
pub(crate) unsafe fn test_g() { | ||
unsafe { g() }; | ||
//~^ ERROR: this call requires target features | ||
} | ||
|
||
#[target_feature(enable = "avx2,pclmulqdq")] | ||
unsafe fn test_h() { | ||
unsafe { h() }; | ||
//~^ ERROR: this call requires target features | ||
} | ||
|
||
#[target_feature(enable = "avx2,pclmulqdq")] | ||
unsafe fn test_h_unsafe() { | ||
unsafe { h() }; | ||
//~^ ERROR: this call requires target features | ||
} | ||
|
||
#[target_feature(enable = "avx2")] | ||
unsafe fn f() -> u32 { | ||
0 | ||
} | ||
|
||
#[target_feature(enable = "avx2,pclmulqdq")] | ||
unsafe fn g() -> u32 { | ||
0 | ||
} | ||
|
||
#[target_feature(enable = "avx2")] | ||
#[target_feature(enable = "pclmulqdq")] | ||
unsafe fn h() -> u32 { | ||
0 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//@only-target: x86_64 | ||
#![allow(clippy::missing_safety_doc)] | ||
|
||
pub fn test_f() { | ||
unsafe { f() }; | ||
//~^ ERROR: this call requires target features | ||
} | ||
|
||
pub(crate) fn test_g() { | ||
unsafe { g() }; | ||
//~^ ERROR: this call requires target features | ||
} | ||
|
||
fn test_h() { | ||
unsafe { h() }; | ||
//~^ ERROR: this call requires target features | ||
} | ||
|
||
unsafe fn test_h_unsafe() { | ||
unsafe { h() }; | ||
//~^ ERROR: this call requires target features | ||
} | ||
|
||
#[target_feature(enable = "avx2")] | ||
unsafe fn f() -> u32 { | ||
0 | ||
} | ||
|
||
#[target_feature(enable = "avx2,pclmulqdq")] | ||
unsafe fn g() -> u32 { | ||
0 | ||
} | ||
|
||
#[target_feature(enable = "avx2")] | ||
#[target_feature(enable = "pclmulqdq")] | ||
unsafe fn h() -> u32 { | ||
0 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
error: this call requires target features that the surrounding function does not enable | ||
--> tests/ui/call_missing_target_feature.rs:5:14 | ||
| | ||
LL | unsafe { f() }; | ||
| ^^^ this function call requires target features to be enabled | ||
| | ||
= note: `-D clippy::call-missing-target-feature` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::call_missing_target_feature)]` | ||
help: add the missing target features to the surrounding function | ||
| | ||
LL + #[target_feature(enable = "avx2")] | ||
LL ~ pub unsafe fn test_f() { | ||
| | ||
|
||
error: this call requires target features that the surrounding function does not enable | ||
--> tests/ui/call_missing_target_feature.rs:10:14 | ||
| | ||
LL | unsafe { g() }; | ||
| ^^^ this function call requires target features to be enabled | ||
| | ||
help: add the missing target features to the surrounding function | ||
| | ||
LL + #[target_feature(enable = "avx2,pclmulqdq")] | ||
LL ~ pub(crate) unsafe fn test_g() { | ||
| | ||
|
||
error: this call requires target features that the surrounding function does not enable | ||
--> tests/ui/call_missing_target_feature.rs:15:14 | ||
| | ||
LL | unsafe { h() }; | ||
| ^^^ this function call requires target features to be enabled | ||
| | ||
help: add the missing target features to the surrounding function | ||
| | ||
LL + #[target_feature(enable = "avx2,pclmulqdq")] | ||
LL ~ unsafe fn test_h() { | ||
| | ||
|
||
error: this call requires target features that the surrounding function does not enable | ||
--> tests/ui/call_missing_target_feature.rs:20:14 | ||
| | ||
LL | unsafe { h() }; | ||
| ^^^ this function call requires target features to be enabled | ||
| | ||
help: add the missing target features to the surrounding function | ||
| | ||
LL + #[target_feature(enable = "avx2,pclmulqdq")] | ||
LL | unsafe fn test_h_unsafe() { | ||
| | ||
|
||
error: aborting due to 4 previous errors | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
shrink_to_lo
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I'll fix that along with the category when the FCP is completed