-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add support for minimum supported rust version #6201
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @flip1995 (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
The right way to do this is to add a configuration option here: rust-clippy/clippy_lints/src/utils/conf.rs Lines 108 to 110 in a675778
In the doc comment above each item, you can list lints, this configuration option applies to, like here: rust-clippy/clippy_lints/src/utils/conf.rs Lines 141 to 142 in a675778
Then, you can pass those configuration options to the rust-clippy/clippy_lints/src/lib.rs Lines 956 to 957 in a675778
|
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.
Just adding the configuration option and then to the structs, won't do anything. You have to also prevent the lint from triggering, if the MSRV is lower than the applicable MSRV for the specific lints.
Also there should be a way to set the MSRV with an attribute, like #![clippy::msrv = "1.40.0"]
. See the cognitive_complexity
attribute on how to do this.
@flip1995 Yep, I'm still working on that. The |
See the |
☔ The latest upstream changes (presumably #6227) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
@flip1995 From what I saw, the |
@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author |
f725450
to
b0c826f
Compare
☔ The latest upstream changes (presumably #6101) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
If the config setting or attribute is not present Clippy should function as before i.e. everything should be linted. |
@mikerite if the attribute is not specified in the configuration a lot of lints will be skipped since the minimum supported version defaults to 1.0.0 |
Yeah if it's not a lot of trouble. Unfortunately I don't have access to a Linux environment atm |
@flip1995 where can we add some documentation about this feature? |
Alright, will do tomorrow.
I thought about doing a blog post in the Rust internals forum, once this hits nightly (so roughly 2 weeks). For the documentation, I would add it to the |
Thank you so much! :)
That sounds good, I'll add some docs about this feature to the readme today |
Can you do this in a separate PR please? I'll merge this once I fixed the dogfood errors. |
Sure thing! |
b1b4019
to
266b799
Compare
clippy_lints/src/utils/mod.rs
Outdated
#[macro_export] | ||
macro_rules! extract_msrv_attr { | ||
(LateContext) => { | ||
fn enter_lint_attrs(&mut self, cx: &rustc_lint::LateContext<'tcx>, attrs: &'tcx [Attribute]) { | ||
match get_inner_attr(cx.sess(), attrs, "msrv") { | ||
Some(msrv_attr) => { | ||
if let Some(msrv) = msrv_attr.value_str() { | ||
self.msrv = crate::utils::parse_msrv(&msrv.to_string(), Some(cx.sess()), Some(msrv_attr.span)); | ||
} else { | ||
cx.sess().span_err(msrv_attr.span, "bad clippy attribute"); | ||
} | ||
}, | ||
_ => (), | ||
} | ||
} | ||
extract_msrv_attr!(@LateContext, ()); | ||
}; | ||
(EarlyContext) => { | ||
fn enter_lint_attrs(&mut self, cx: &rustc_lint::EarlyContext<'tcx>, attrs: &'tcx [Attribute]) { | ||
match get_inner_attr(cx.sess, attrs, "msrv") { | ||
extract_msrv_attr!(@EarlyContext); | ||
}; | ||
(@$context:ident$(, $call:tt)?) => { | ||
fn enter_lint_attrs(&mut self, cx: &rustc_lint::$context<'tcx>, attrs: &'tcx [rustc_ast::ast::Attribute]) { | ||
use $crate::utils::get_unique_inner_attr; |
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.
@suyash458 This change might be interesting to you, for learning about macro magic. I did 3 things here:
- I removed
macro_export
, because we don't want this macro to be used outside of Clippy - I added the
use $crate::utils::get_unique_inner_attr;
statement, so that this function does not have to be imported in very module this is used. - I added an "internal rule". Note that there isn't a thing like an "internal rule" officially, but there is a convention that rules starting with
@
should only be used inside the macro definition. With this and the(, $call:tt)?
thing I was able to deduplicate the code. The?
operator in a macro means, that this either should appear once or not at all.tt
means, that$call
is a token tree, so basically everything that is recognized as a token by the Rust lexer is allowed. In combination this makes passing()
into the macro possible and can be added anywhere.
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.
@flip1995 Thanks, this is interesting! I tried making this generic using trait objects but quickly ran into a lot of issues with lifetimes
add configuration option for minimum supported rust version add msrv attribute to some lints listed in rust-lang#6097 add tests
@bors r+ rollup=never p=10 @suyash458 Thanks for all your work on this! What's left to do is:
|
📌 Commit d06076c has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Sounds great! I can work on 1 and 3 in separate PRs |
@@ -130,7 +130,9 @@ pub fn get_unique_inner_attr(sess: &Session, attrs: &[ast::Attribute], name: &'s | |||
match attr.style { | |||
ast::AttrStyle::Inner if unique_attr.is_none() => unique_attr = Some(attr.clone()), |
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.
This is pretty neat, didn't realize match
arms can have conditions
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.
Oh, matches are really powerful, take a look at some examples here, to see what you can do with match expressions: https://doc.rust-lang.org/rust-by-example/flow_control/match.html (This section has multiple nested subsections)
Add MSRV to more lints specified in #6097 add MSRV to more lints specified in #6097 add instructions for adding msrv in other lints update tests - [x] `redundant_field_names` requires Rust 1.17 due to suggest feature stablized in that version. - [x] `redundant_static_lifetimes` requires Rust 1.17 due to suggest feature stablized in that version. - [x] `filter_map_next` requires Rust 1.30 due to suggest `Iterator::find_map`. - [x] `checked_conversions` requires Rust 1.34 due to suggest `TryFrom`. - [x] `match_like_matches_macro` requires Rust 1.42 due to suggest `matches!`. Addressed in #6201 - [x] `manual_strip` requires Rust 1.45 due to suggest `str::{strip_prefix, strip_suffix}`. Addressed in #6201 - [x] `option_as_ref_deref` requires Rust 1.40 due to suggest `Option::{as_deref, as_deref_mut}`. Addressed in #6201 - [x] `manual_non_exhaustive` requires Rust 1.40 due to suggest `#[non_exhaustive]`. Addressed in #6201 - [x] `manual_range_contains` requires Rust 1.35 due to suggest `Range*::contains`. - [x] `use_self` requires Rust 1.37 due to suggest `Self::Variant on enum`. - [x] `mem_replace_with_default` requires Rust 1.40 due to suggest `mem::take`. - [x] `map_unwrap_or` requires Rust 1.41 due to suggest `Result::{map_or, map_or_else}`. - [x] `missing_const_for_fn` requires Rust 1.46 due to `match/if/loop in const fn` needs that version. changelog: Add MSRV config to more lints. ^This is now the complete list, AFAWK
add configuration option for minimum supported rust version
add msrv attribute to some lints listed in #6097
add tests
addresses #6097
changelog: Add
msrv
configuration to Clippy. This should get a longer changelog entry.