Skip to content

Allow using clippy::msrv as an outer attribute #9860

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

Merged
merged 1 commit into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ in the `Cargo.toml` can be used.
rust-version = "1.30"
```

The MSRV can also be specified as an inner attribute, like below.
The MSRV can also be specified as an attribute, like below.

```rust
#![feature(custom_inner_attributes)]
Expand Down
2 changes: 1 addition & 1 deletion book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ minimum supported Rust version (MSRV) in the clippy configuration file.
msrv = "1.30.0"
```

The MSRV can also be specified as an inner attribute, like below.
The MSRV can also be specified as an attribute, like below.

```rust
#![feature(custom_inner_attributes)]
Expand Down
10 changes: 3 additions & 7 deletions book/src/development/adding_lints.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ if !self.msrv.meets(msrvs::STR_STRIP_PREFIX) {
}
```

The project's MSRV can also be specified as an inner attribute, which overrides
The project's MSRV can also be specified as an attribute, which overrides
the value from `clippy.toml`. This can be accounted for using the
`extract_msrv_attr!(LintContext)` macro and passing
`LateContext`/`EarlyContext`.
Expand All @@ -483,19 +483,15 @@ have a case for the version below the MSRV and one with the same contents but
for the MSRV version itself.

```rust
#![feature(custom_inner_attributes)]

...

#[clippy::msrv = "1.44"]
fn msrv_1_44() {
#![clippy::msrv = "1.44"]

/* something that would trigger the lint */
}

#[clippy::msrv = "1.45"]
fn msrv_1_45() {
#![clippy::msrv = "1.45"]

/* something that would trigger the lint */
}
```
Expand Down
24 changes: 12 additions & 12 deletions clippy_utils/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,19 @@ fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'
}
}

pub fn get_unique_inner_attr(sess: &Session, attrs: &[ast::Attribute], name: &'static str) -> Option<ast::Attribute> {
let mut unique_attr = None;
pub fn get_unique_attr<'a>(
sess: &'a Session,
attrs: &'a [ast::Attribute],
name: &'static str,
) -> Option<&'a ast::Attribute> {
let mut unique_attr: Option<&ast::Attribute> = None;
for attr in get_attr(sess, attrs, name) {
match attr.style {
ast::AttrStyle::Inner if unique_attr.is_none() => unique_attr = Some(attr.clone()),
ast::AttrStyle::Inner => {
sess.struct_span_err(attr.span, &format!("`{name}` is defined multiple times"))
.span_note(unique_attr.as_ref().unwrap().span, "first definition found here")
.emit();
},
ast::AttrStyle::Outer => {
sess.span_err(attr.span, format!("`{name}` cannot be an outer attribute"));
},
if let Some(duplicate) = unique_attr {
sess.struct_span_err(attr.span, &format!("`{name}` is defined multiple times"))
.span_note(duplicate.span, "first definition found here")
.emit();
} else {
unique_attr = Some(attr);
}
}
unique_attr
Expand Down
4 changes: 2 additions & 2 deletions clippy_utils/src/msrvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_semver::RustcVersion;
use rustc_session::Session;
use rustc_span::Span;

use crate::attrs::get_unique_inner_attr;
use crate::attrs::get_unique_attr;

macro_rules! msrv_aliases {
($($major:literal,$minor:literal,$patch:literal {
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Msrv {
}

fn parse_attr(sess: &Session, attrs: &[Attribute]) -> Option<RustcVersion> {
if let Some(msrv_attr) = get_unique_inner_attr(sess, attrs, "msrv") {
if let Some(msrv_attr) = get_unique_attr(sess, attrs, "msrv") {
if let Some(msrv) = msrv_attr.value_str() {
return parse_msrv(&msrv.to_string(), Some(sess), Some(msrv_attr.span));
}
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/almost_complete_letter_range.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// edition:2018
// aux-build:macro_rules.rs

#![feature(custom_inner_attributes)]
#![feature(exclusive_range_pattern)]
#![feature(stmt_expr_attributes)]
#![warn(clippy::almost_complete_letter_range)]
Expand Down Expand Up @@ -62,16 +61,16 @@ fn main() {
b!();
}

#[clippy::msrv = "1.25"]
fn _under_msrv() {
#![clippy::msrv = "1.25"]
let _ = match 'a' {
'a'...'z' => 1,
_ => 2,
};
}

#[clippy::msrv = "1.26"]
fn _meets_msrv() {
#![clippy::msrv = "1.26"]
let _ = 'a'..='z';
let _ = match 'a' {
'a'..='z' => 1,
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/almost_complete_letter_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// edition:2018
// aux-build:macro_rules.rs

#![feature(custom_inner_attributes)]
#![feature(exclusive_range_pattern)]
#![feature(stmt_expr_attributes)]
#![warn(clippy::almost_complete_letter_range)]
Expand Down Expand Up @@ -62,16 +61,16 @@ fn main() {
b!();
}

#[clippy::msrv = "1.25"]
fn _under_msrv() {
#![clippy::msrv = "1.25"]
let _ = match 'a' {
'a'..'z' => 1,
_ => 2,
};
}

#[clippy::msrv = "1.26"]
fn _meets_msrv() {
#![clippy::msrv = "1.26"]
let _ = 'a'..'z';
let _ = match 'a' {
'a'..'z' => 1,
Expand Down
26 changes: 13 additions & 13 deletions tests/ui/almost_complete_letter_range.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:30:17
--> $DIR/almost_complete_letter_range.rs:29:17
|
LL | let _ = ('a') ..'z';
| ^^^^^^--^^^
Expand All @@ -9,71 +9,71 @@ LL | let _ = ('a') ..'z';
= note: `-D clippy::almost-complete-letter-range` implied by `-D warnings`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:31:17
--> $DIR/almost_complete_letter_range.rs:30:17
|
LL | let _ = 'A' .. ('Z');
| ^^^^--^^^^^^
| |
| help: use an inclusive range: `..=`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:37:13
--> $DIR/almost_complete_letter_range.rs:36:13
|
LL | let _ = (b'a')..(b'z');
| ^^^^^^--^^^^^^
| |
| help: use an inclusive range: `..=`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:38:13
--> $DIR/almost_complete_letter_range.rs:37:13
|
LL | let _ = b'A'..b'Z';
| ^^^^--^^^^
| |
| help: use an inclusive range: `..=`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:43:13
--> $DIR/almost_complete_letter_range.rs:42:13
|
LL | let _ = a!()..'z';
| ^^^^--^^^
| |
| help: use an inclusive range: `..=`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:46:9
--> $DIR/almost_complete_letter_range.rs:45:9
|
LL | b'a'..b'z' if true => 1,
| ^^^^--^^^^
| |
| help: use an inclusive range: `..=`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:47:9
--> $DIR/almost_complete_letter_range.rs:46:9
|
LL | b'A'..b'Z' if true => 2,
| ^^^^--^^^^
| |
| help: use an inclusive range: `..=`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:54:9
--> $DIR/almost_complete_letter_range.rs:53:9
|
LL | 'a'..'z' if true => 1,
| ^^^--^^^
| |
| help: use an inclusive range: `..=`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:55:9
--> $DIR/almost_complete_letter_range.rs:54:9
|
LL | 'A'..'Z' if true => 2,
| ^^^--^^^
| |
| help: use an inclusive range: `..=`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:23:17
--> $DIR/almost_complete_letter_range.rs:22:17
|
LL | let _ = 'a'..'z';
| ^^^--^^^
Expand All @@ -86,23 +86,23 @@ LL | b!();
= note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info)

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:68:9
--> $DIR/almost_complete_letter_range.rs:67:9
|
LL | 'a'..'z' => 1,
| ^^^--^^^
| |
| help: use an inclusive range: `...`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:75:13
--> $DIR/almost_complete_letter_range.rs:74:13
|
LL | let _ = 'a'..'z';
| ^^^--^^^
| |
| help: use an inclusive range: `..=`

error: almost complete ascii letter range
--> $DIR/almost_complete_letter_range.rs:77:9
--> $DIR/almost_complete_letter_range.rs:76:9
|
LL | 'a'..'z' => 1,
| ^^^--^^^
Expand Down
7 changes: 2 additions & 5 deletions tests/ui/cast_abs_to_unsigned.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// run-rustfix

#![feature(custom_inner_attributes)]
#![warn(clippy::cast_abs_to_unsigned)]
#![allow(clippy::uninlined_format_args, unused)]

Expand Down Expand Up @@ -33,16 +32,14 @@ fn main() {
let _ = (x as i64 - y as i64).unsigned_abs() as u32;
}

#[clippy::msrv = "1.50"]
fn msrv_1_50() {
#![clippy::msrv = "1.50"]

let x: i32 = 10;
assert_eq!(10u32, x.abs() as u32);
}

#[clippy::msrv = "1.51"]
fn msrv_1_51() {
#![clippy::msrv = "1.51"]

let x: i32 = 10;
assert_eq!(10u32, x.unsigned_abs());
}
7 changes: 2 additions & 5 deletions tests/ui/cast_abs_to_unsigned.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// run-rustfix

#![feature(custom_inner_attributes)]
#![warn(clippy::cast_abs_to_unsigned)]
#![allow(clippy::uninlined_format_args, unused)]

Expand Down Expand Up @@ -33,16 +32,14 @@ fn main() {
let _ = (x as i64 - y as i64).abs() as u32;
}

#[clippy::msrv = "1.50"]
fn msrv_1_50() {
#![clippy::msrv = "1.50"]

let x: i32 = 10;
assert_eq!(10u32, x.abs() as u32);
}

#[clippy::msrv = "1.51"]
fn msrv_1_51() {
#![clippy::msrv = "1.51"]

let x: i32 = 10;
assert_eq!(10u32, x.abs() as u32);
}
Loading