new lint: add call_missing_target_feature
lint
#13240
Open
+303
−0
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.
changelog: [
call_missing_target_feature
]: was addedThis PR adds a lint for missing target features.
Motivation
A function that requires a target feature (e.g.
#[target_feature(enable = "avx2")]
to indicate that avx2 instructions can be used) must be called from a context where these instructions are actually available. That check is part of the safety contract of the function. For correctness and performance, any function calling a function with a target feature must eitherHowever, currently no warning is emitted when the user forgets to correctly annotate target features. My personal experience with zlib-rs is that it's just really easy to forget the annotation on some helper function, and to forget to check for target features in code review. Hence, this check.
Implementation
The implementation looks for function calls, then looks up if that function (the callee) has any target features, and if so, checks whether the caller also has these target features. Any missing target features are reported.
This lint is not always accurate, and I think should be off by default, but is very useful in e.g. simd-heavy codebases. Some
#[cfg(allow(...)]
are always required though in the function where the feature detection macro is called.Questions
The tests run into
despite the suggestion being
Applicability::MaybeIncorrect
. I must be missing something here. The problem is that the function must also be marked as unsafe in this case. Still I think the suggestion is useful. Also inserting the unsafe seems complicated, but maybe that is possible?cc @bjorn3 maybe you have phrasing suggestions?