Check for MSRV attributes in late passes using the HIR

This commit is contained in:
Alex Macleod
2024-12-13 17:40:07 +00:00
parent 2cdb90d961
commit 0972c3b565
12 changed files with 246 additions and 214 deletions

View File

@@ -460,7 +460,7 @@ pub struct ManualStrip {
impl ManualStrip { impl ManualStrip {
pub fn new(conf: &'static Conf) -> Self { pub fn new(conf: &'static Conf) -> Self {
Self { msrv: conf.msrv.clone() } Self { msrv: conf.msrv }
} }
} }
``` ```
@@ -469,24 +469,13 @@ The project's MSRV can then be matched against the feature MSRV in the LintPass
using the `Msrv::meets` method. using the `Msrv::meets` method.
``` rust ``` rust
if !self.msrv.meets(msrvs::STR_STRIP_PREFIX) { if !self.msrv.meets(cx, msrvs::STR_STRIP_PREFIX) {
return; return;
} }
``` ```
The project's MSRV can also be specified as an attribute, which overrides Early lint passes should instead use `MsrvStack` coupled with
the value from `clippy.toml`. This can be accounted for using the `extract_msrv_attr!()`
`extract_msrv_attr!(LintContext)` macro and passing
`LateContext`/`EarlyContext`.
```rust,ignore
impl<'tcx> LateLintPass<'tcx> for ManualStrip {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
...
}
extract_msrv_attr!(LateContext);
}
```
Once the `msrv` is added to the lint, a relevant test case should be added to Once the `msrv` is added to the lint, a relevant test case should be added to
the lint's test file, `tests/ui/manual_strip.rs` in this example. It should the lint's test file, `tests/ui/manual_strip.rs` in this example. It should
@@ -512,8 +501,16 @@ in `clippy_config/src/conf.rs`:
```rust ```rust
define_Conf! { define_Conf! {
/// Lint: LIST, OF, LINTS, <THE_NEWLY_ADDED_LINT>. The minimum rust version that the project supports #[lints(
(msrv: Option<String> = None), allow_attributes,
allow_attributes_without_reason,
..
<the newly added lint name>,
..
unused_trait_names,
use_self,
)]
msrv: Msrv = Msrv::default(),
... ...
} }
``` ```

View File

@@ -670,7 +670,7 @@ define_Conf! {
unused_trait_names, unused_trait_names,
use_self, use_self,
)] )]
msrv: Msrv = Msrv::empty(), msrv: Msrv = Msrv::default(),
/// The minimum size (in bytes) to consider a type for passing by reference instead of by value. /// The minimum size (in bytes) to consider a type for passing by reference instead of by value.
#[lints(large_types_passed_by_value)] #[lints(large_types_passed_by_value)]
pass_by_value_size_limit: u64 = 256, pass_by_value_size_limit: u64 = 256,

View File

@@ -35,7 +35,7 @@ fn main() {
category, category,
r#type, r#type,
msrv, msrv,
} => match new_lint::create(&pass, &name, &category, r#type.as_deref(), msrv) { } => match new_lint::create(pass, &name, &category, r#type.as_deref(), msrv) {
Ok(()) => update_lints::update(utils::UpdateMode::Change), Ok(()) => update_lints::update(utils::UpdateMode::Change),
Err(e) => eprintln!("Unable to create lint: {e}"), Err(e) => eprintln!("Unable to create lint: {e}"),
}, },
@@ -147,9 +147,9 @@ enum DevCommand {
#[command(name = "new_lint")] #[command(name = "new_lint")]
/// Create a new lint and run `cargo dev update_lints` /// Create a new lint and run `cargo dev update_lints`
NewLint { NewLint {
#[arg(short, long, value_parser = ["early", "late"], conflicts_with = "type", default_value = "late")] #[arg(short, long, conflicts_with = "type", default_value = "late")]
/// Specify whether the lint runs during the early or late pass /// Specify whether the lint runs during the early or late pass
pass: String, pass: new_lint::Pass,
#[arg( #[arg(
short, short,
long, long,

View File

@@ -1,13 +1,28 @@
use crate::utils::{clippy_project_root, clippy_version}; use crate::utils::{clippy_project_root, clippy_version};
use clap::ValueEnum;
use indoc::{formatdoc, writedoc}; use indoc::{formatdoc, writedoc};
use std::fmt::Write as _; use std::fmt::{self, Write as _};
use std::fs::{self, OpenOptions}; use std::fs::{self, OpenOptions};
use std::io::prelude::*; use std::io::{self, Write as _};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::{fmt, io};
#[derive(Clone, Copy, PartialEq, ValueEnum)]
pub enum Pass {
Early,
Late,
}
impl fmt::Display for Pass {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Pass::Early => "early",
Pass::Late => "late",
})
}
}
struct LintData<'a> { struct LintData<'a> {
pass: &'a str, pass: Pass,
name: &'a str, name: &'a str,
category: &'a str, category: &'a str,
ty: Option<&'a str>, ty: Option<&'a str>,
@@ -35,7 +50,7 @@ impl<T> Context for io::Result<T> {
/// # Errors /// # Errors
/// ///
/// This function errors out if the files couldn't be created or written to. /// This function errors out if the files couldn't be created or written to.
pub fn create(pass: &str, name: &str, category: &str, mut ty: Option<&str>, msrv: bool) -> io::Result<()> { pub fn create(pass: Pass, name: &str, category: &str, mut ty: Option<&str>, msrv: bool) -> io::Result<()> {
if category == "cargo" && ty.is_none() { if category == "cargo" && ty.is_none() {
// `cargo` is a special category, these lints should always be in `clippy_lints/src/cargo` // `cargo` is a special category, these lints should always be in `clippy_lints/src/cargo`
ty = Some("cargo"); ty = Some("cargo");
@@ -56,7 +71,7 @@ pub fn create(pass: &str, name: &str, category: &str, mut ty: Option<&str>, msrv
add_lint(&lint, msrv).context("Unable to add lint to clippy_lints/src/lib.rs")?; add_lint(&lint, msrv).context("Unable to add lint to clippy_lints/src/lib.rs")?;
} }
if pass == "early" { if pass == Pass::Early {
println!( println!(
"\n\ "\n\
NOTE: Use a late pass unless you need something specific from\n\ NOTE: Use a late pass unless you need something specific from\n\
@@ -136,23 +151,17 @@ fn add_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
let mut lib_rs = fs::read_to_string(path).context("reading")?; let mut lib_rs = fs::read_to_string(path).context("reading")?;
let comment_start = lib_rs.find("// add lints here,").expect("Couldn't find comment"); let comment_start = lib_rs.find("// add lints here,").expect("Couldn't find comment");
let ctor_arg = if lint.pass == Pass::Late { "_" } else { "" };
let lint_pass = lint.pass;
let module_name = lint.name;
let camel_name = to_camel_case(lint.name);
let new_lint = if enable_msrv { let new_lint = if enable_msrv {
format!( format!(
"store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(conf)));\n ", "store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(conf)));\n ",
lint_pass = lint.pass,
ctor_arg = if lint.pass == "late" { "_" } else { "" },
module_name = lint.name,
camel_name = to_camel_case(lint.name),
) )
} else { } else {
format!( format!("store.register_{lint_pass}_pass(|{ctor_arg}| Box::new({module_name}::{camel_name}));\n ",)
"store.register_{lint_pass}_pass(|{ctor_arg}| Box::new({module_name}::{camel_name}));\n ",
lint_pass = lint.pass,
ctor_arg = if lint.pass == "late" { "_" } else { "" },
module_name = lint.name,
camel_name = to_camel_case(lint.name),
)
}; };
lib_rs.insert_str(comment_start, &new_lint); lib_rs.insert_str(comment_start, &new_lint);
@@ -242,11 +251,16 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
let mut result = String::new(); let mut result = String::new();
let (pass_type, pass_lifetimes, pass_import, context_import) = match lint.pass { let (pass_type, pass_lifetimes, pass_import, context_import) = match lint.pass {
"early" => ("EarlyLintPass", "", "use rustc_ast::ast::*;", "EarlyContext"), Pass::Early => ("EarlyLintPass", "", "use rustc_ast::ast::*;", "EarlyContext"),
"late" => ("LateLintPass", "<'_>", "use rustc_hir::*;", "LateContext"), Pass::Late => ("LateLintPass", "<'_>", "use rustc_hir::*;", "LateContext"),
_ => { };
unreachable!("`pass_type` should only ever be `early` or `late`!"); let (msrv_ty, msrv_ctor, extract_msrv) = match lint.pass {
}, Pass::Early => (
"MsrvStack",
"MsrvStack::new(conf.msrv)",
"\n extract_msrv_attr!();\n",
),
Pass::Late => ("Msrv", "conf.msrv", ""),
}; };
let lint_name = lint.name; let lint_name = lint.name;
@@ -258,10 +272,10 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
let _: fmt::Result = writedoc!( let _: fmt::Result = writedoc!(
result, result,
r" r"
use clippy_utils::msrvs::{{self, Msrv}}; use clippy_utils::msrvs::{{self, {msrv_ty}}};
use clippy_config::Conf; use clippy_config::Conf;
{pass_import} {pass_import}
use rustc_lint::{{{context_import}, {pass_type}, LintContext}}; use rustc_lint::{{{context_import}, {pass_type}}};
use rustc_session::impl_lint_pass; use rustc_session::impl_lint_pass;
" "
@@ -285,20 +299,18 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
result, result,
r" r"
pub struct {name_camel} {{ pub struct {name_camel} {{
msrv: Msrv, msrv: {msrv_ty},
}} }}
impl {name_camel} {{ impl {name_camel} {{
pub fn new(conf: &'static Conf) -> Self {{ pub fn new(conf: &'static Conf) -> Self {{
Self {{ msrv: conf.msrv.clone() }} Self {{ msrv: {msrv_ctor} }}
}} }}
}} }}
impl_lint_pass!({name_camel} => [{name_upper}]); impl_lint_pass!({name_camel} => [{name_upper}]);
impl {pass_type}{pass_lifetimes} for {name_camel} {{ impl {pass_type}{pass_lifetimes} for {name_camel} {{{extract_msrv}}}
extract_msrv_attr!({context_import});
}}
// TODO: Add MSRV level to `clippy_config/src/msrvs.rs` if needed. // TODO: Add MSRV level to `clippy_config/src/msrvs.rs` if needed.
// TODO: Update msrv config comment in `clippy_config/src/conf.rs` // TODO: Update msrv config comment in `clippy_config/src/conf.rs`
@@ -375,9 +387,9 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
let mod_file_path = ty_dir.join("mod.rs"); let mod_file_path = ty_dir.join("mod.rs");
let context_import = setup_mod_file(&mod_file_path, lint)?; let context_import = setup_mod_file(&mod_file_path, lint)?;
let pass_lifetimes = match context_import { let (pass_lifetimes, msrv_ty, msrv_ref, msrv_cx) = match context_import {
"LateContext" => "<'_>", "LateContext" => ("<'_>", "Msrv", "", "cx, "),
_ => "", _ => ("", "MsrvStack", "&", ""),
}; };
let name_upper = lint.name.to_uppercase(); let name_upper = lint.name.to_uppercase();
@@ -387,14 +399,14 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
let _: fmt::Result = writedoc!( let _: fmt::Result = writedoc!(
lint_file_contents, lint_file_contents,
r#" r#"
use clippy_utils::msrvs::{{self, Msrv}}; use clippy_utils::msrvs::{{self, {msrv_ty}}};
use rustc_lint::{{{context_import}, LintContext}}; use rustc_lint::{{{context_import}, LintContext}};
use super::{name_upper}; use super::{name_upper};
// TODO: Adjust the parameters as necessary // TODO: Adjust the parameters as necessary
pub(super) fn check(cx: &{context_import}{pass_lifetimes}, msrv: &Msrv) {{ pub(super) fn check(cx: &{context_import}{pass_lifetimes}, msrv: {msrv_ref}{msrv_ty}) {{
if !msrv.meets(todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{ if !msrv.meets({msrv_cx}todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{
return; return;
}} }}
todo!(); todo!();

View File

@@ -137,24 +137,13 @@ use rustc_middle::hir::nested_filter;
#[macro_export] #[macro_export]
macro_rules! extract_msrv_attr { macro_rules! extract_msrv_attr {
(LateContext) => { () => {
fn check_attributes(&mut self, cx: &rustc_lint::LateContext<'_>, attrs: &[rustc_hir::Attribute]) { fn check_attributes(&mut self, cx: &rustc_lint::EarlyContext<'_>, attrs: &[rustc_ast::ast::Attribute]) {
let sess = rustc_lint::LintContext::sess(cx); let sess = rustc_lint::LintContext::sess(cx);
self.msrv.check_attributes(sess, attrs); self.msrv.check_attributes(sess, attrs);
} }
fn check_attributes_post(&mut self, cx: &rustc_lint::LateContext<'_>, attrs: &[rustc_hir::Attribute]) { fn check_attributes_post(&mut self, cx: &rustc_lint::EarlyContext<'_>, attrs: &[rustc_ast::ast::Attribute]) {
let sess = rustc_lint::LintContext::sess(cx);
self.msrv.check_attributes_post(sess, attrs);
}
};
(EarlyContext) => {
fn check_attributes(&mut self, cx: &rustc_lint::EarlyContext<'_>, attrs: &[rustc_ast::Attribute]) {
let sess = rustc_lint::LintContext::sess(cx);
self.msrv.check_attributes(sess, attrs);
}
fn check_attributes_post(&mut self, cx: &rustc_lint::EarlyContext<'_>, attrs: &[rustc_ast::Attribute]) {
let sess = rustc_lint::LintContext::sess(cx); let sess = rustc_lint::LintContext::sess(cx);
self.msrv.check_attributes_post(sess, attrs); self.msrv.check_attributes_post(sess, attrs);
} }

View File

@@ -1,11 +1,14 @@
use rustc_ast::Attribute;
use rustc_ast::attr::AttributeExt; use rustc_ast::attr::AttributeExt;
use rustc_attr_parsing::{RustcVersion, parse_version}; use rustc_attr_parsing::{RustcVersion, parse_version};
use rustc_lint::LateContext;
use rustc_session::Session; use rustc_session::Session;
use rustc_span::{Symbol, sym}; use rustc_span::{Symbol, sym};
use serde::Deserialize; use serde::Deserialize;
use smallvec::{SmallVec, smallvec}; use smallvec::SmallVec;
use std::fmt; use std::iter::once;
use std::sync::atomic::{AtomicBool, Ordering};
macro_rules! msrv_aliases { macro_rules! msrv_aliases {
($($major:literal,$minor:literal,$patch:literal { ($($major:literal,$minor:literal,$patch:literal {
@@ -73,21 +76,15 @@ msrv_aliases! {
1,15,0 { MAYBE_BOUND_IN_WHERE } 1,15,0 { MAYBE_BOUND_IN_WHERE }
} }
/// Tracks the current MSRV from `clippy.toml`, `Cargo.toml` or set via `#[clippy::msrv]` /// `#[clippy::msrv]` attributes are rarely used outside of Clippy's test suite, as a basic
#[derive(Debug, Clone)] /// optimization we can skip traversing the HIR in [`Msrv::meets`] if we never saw an MSRV attribute
pub struct Msrv { /// during the early lint passes
stack: SmallVec<[RustcVersion; 2]>, static SEEN_MSRV_ATTR: AtomicBool = AtomicBool::new(false);
}
impl fmt::Display for Msrv { /// Tracks the current MSRV from `clippy.toml`, `Cargo.toml` or set via `#[clippy::msrv]` in late
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// lint passes, use [`MsrvStack`] for early passes
if let Some(msrv) = self.current() { #[derive(Copy, Clone, Debug, Default)]
write!(f, "{msrv}") pub struct Msrv(Option<RustcVersion>);
} else {
f.write_str("1.0.0")
}
}
}
impl<'de> Deserialize<'de> for Msrv { impl<'de> Deserialize<'de> for Msrv {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -96,14 +93,36 @@ impl<'de> Deserialize<'de> for Msrv {
{ {
let v = String::deserialize(deserializer)?; let v = String::deserialize(deserializer)?;
parse_version(Symbol::intern(&v)) parse_version(Symbol::intern(&v))
.map(|v| Msrv { stack: smallvec![v] }) .map(|v| Self(Some(v)))
.ok_or_else(|| serde::de::Error::custom("not a valid Rust version")) .ok_or_else(|| serde::de::Error::custom("not a valid Rust version"))
} }
} }
impl Msrv { impl Msrv {
pub fn empty() -> Msrv { /// Returns the MSRV at the current node
Msrv { stack: SmallVec::new() } ///
/// If the crate being linted uses an `#[clippy::msrv]` attribute this will search the parent
/// nodes for that attribute, prefer to run this check after cheaper pattern matching operations
pub fn current(self, cx: &LateContext<'_>) -> Option<RustcVersion> {
if SEEN_MSRV_ATTR.load(Ordering::Relaxed) {
let start = cx.last_node_with_lint_attrs;
if let Some(msrv_attr) = once(start)
.chain(cx.tcx.hir_parent_id_iter(start))
.find_map(|id| parse_attrs(cx.tcx.sess, cx.tcx.hir().attrs(id)))
{
return Some(msrv_attr);
}
}
self.0
}
/// Checks if a required version from [this module](self) is met at the current node
///
/// If the crate being linted uses an `#[clippy::msrv]` attribute this will search the parent
/// nodes for that attribute, prefer to run this check after cheaper pattern matching operations
pub fn meets(self, cx: &LateContext<'_>, required: RustcVersion) -> bool {
self.current(cx).is_none_or(|msrv| msrv >= required)
} }
pub fn read_cargo(&mut self, sess: &Session) { pub fn read_cargo(&mut self, sess: &Session) {
@@ -111,8 +130,8 @@ impl Msrv {
.ok() .ok()
.and_then(|v| parse_version(Symbol::intern(&v))); .and_then(|v| parse_version(Symbol::intern(&v)));
match (self.current(), cargo_msrv) { match (self.0, cargo_msrv) {
(None, Some(cargo_msrv)) => self.stack = smallvec![cargo_msrv], (None, Some(cargo_msrv)) => self.0 = Some(cargo_msrv),
(Some(clippy_msrv), Some(cargo_msrv)) => { (Some(clippy_msrv), Some(cargo_msrv)) => {
if clippy_msrv != cargo_msrv { if clippy_msrv != cargo_msrv {
sess.dcx().warn(format!( sess.dcx().warn(format!(
@@ -123,6 +142,21 @@ impl Msrv {
_ => {}, _ => {},
} }
} }
}
/// Tracks the current MSRV from `clippy.toml`, `Cargo.toml` or set via `#[clippy::msrv]` in early
/// lint passes, use [`Msrv`] for late passes
#[derive(Debug, Clone)]
pub struct MsrvStack {
stack: SmallVec<[RustcVersion; 2]>,
}
impl MsrvStack {
pub fn new(initial: Msrv) -> Self {
Self {
stack: SmallVec::from_iter(initial.0),
}
}
pub fn current(&self) -> Option<RustcVersion> { pub fn current(&self) -> Option<RustcVersion> {
self.stack.last().copied() self.stack.last().copied()
@@ -132,7 +166,21 @@ impl Msrv {
self.current().is_none_or(|msrv| msrv >= required) self.current().is_none_or(|msrv| msrv >= required)
} }
fn parse_attr(sess: &Session, attrs: &[impl AttributeExt]) -> Option<RustcVersion> { pub fn check_attributes(&mut self, sess: &Session, attrs: &[Attribute]) {
if let Some(version) = parse_attrs(sess, attrs) {
SEEN_MSRV_ATTR.store(true, Ordering::Relaxed);
self.stack.push(version);
}
}
pub fn check_attributes_post(&mut self, sess: &Session, attrs: &[Attribute]) {
if parse_attrs(sess, attrs).is_some() {
self.stack.pop();
}
}
}
fn parse_attrs(sess: &Session, attrs: &[impl AttributeExt]) -> Option<RustcVersion> {
let sym_msrv = Symbol::intern("msrv"); let sym_msrv = Symbol::intern("msrv");
let mut msrv_attrs = attrs.iter().filter(|attr| attr.path_matches(&[sym::clippy, sym_msrv])); let mut msrv_attrs = attrs.iter().filter(|attr| attr.path_matches(&[sym::clippy, sym_msrv]));
@@ -158,16 +206,3 @@ impl Msrv {
None None
} }
pub fn check_attributes(&mut self, sess: &Session, attrs: &[impl AttributeExt]) {
if let Some(version) = Self::parse_attr(sess, attrs) {
self.stack.push(version);
}
}
pub fn check_attributes_post(&mut self, sess: &Session, attrs: &[impl AttributeExt]) {
if Self::parse_attr(sess, attrs).is_some() {
self.stack.pop();
}
}
}

View File

@@ -19,7 +19,6 @@ pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"];
pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"]; pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"];
pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"]; pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"];
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"]; pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
pub const LATE_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "LateLintPass"];
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"]; pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];
pub const SYMBOL: [&str; 3] = ["rustc_span", "symbol", "Symbol"]; pub const SYMBOL: [&str; 3] = ["rustc_span", "symbol", "Symbol"];
pub const SYMBOL_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Symbol", "as_str"]; pub const SYMBOL_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Symbol", "as_str"];
@@ -33,7 +32,7 @@ pub const IO_ERROR_NEW: [&str; 5] = ["std", "io", "error", "Error", "new"];
pub const IO_ERRORKIND_OTHER: [&str; 5] = ["std", "io", "error", "ErrorKind", "Other"]; pub const IO_ERRORKIND_OTHER: [&str; 5] = ["std", "io", "error", "ErrorKind", "Other"];
// Paths in clippy itself // Paths in clippy itself
pub const MSRV: [&str; 3] = ["clippy_utils", "msrvs", "Msrv"]; pub const MSRV_STACK: [&str; 3] = ["clippy_utils", "msrvs", "MsrvStack"];
// Paths in external crates // Paths in external crates
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates

View File

@@ -11,6 +11,7 @@ use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::Obligation; use rustc_infer::traits::Obligation;
use rustc_lint::LateContext;
use rustc_middle::mir::{ use rustc_middle::mir::{
Body, CastKind, NonDivergingIntrinsic, NullOp, Operand, Place, ProjectionElem, Rvalue, Statement, StatementKind, Body, CastKind, NonDivergingIntrinsic, NullOp, Operand, Place, ProjectionElem, Rvalue, Statement, StatementKind,
Terminator, TerminatorKind, Terminator, TerminatorKind,
@@ -25,16 +26,16 @@ use std::borrow::Cow;
type McfResult = Result<(), (Span, Cow<'static, str>)>; type McfResult = Result<(), (Span, Cow<'static, str>)>;
pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv) -> McfResult { pub fn is_min_const_fn<'tcx>(cx: &LateContext<'tcx>, body: &Body<'tcx>, msrv: Msrv) -> McfResult {
let def_id = body.source.def_id(); let def_id = body.source.def_id();
for local in &body.local_decls { for local in &body.local_decls {
check_ty(tcx, local.ty, local.source_info.span, msrv)?; check_ty(cx, local.ty, local.source_info.span, msrv)?;
} }
// impl trait is gone in MIR, so check the return type manually // impl trait is gone in MIR, so check the return type manually
check_ty( check_ty(
tcx, cx,
tcx.fn_sig(def_id).instantiate_identity().output().skip_binder(), cx.tcx.fn_sig(def_id).instantiate_identity().output().skip_binder(),
body.local_decls.iter().next().unwrap().source_info.span, body.local_decls.iter().next().unwrap().source_info.span,
msrv, msrv,
)?; )?;
@@ -43,16 +44,16 @@ pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv)
// Cleanup blocks are ignored entirely by const eval, so we can too: // Cleanup blocks are ignored entirely by const eval, so we can too:
// https://github.com/rust-lang/rust/blob/1dea922ea6e74f99a0e97de5cdb8174e4dea0444/compiler/rustc_const_eval/src/transform/check_consts/check.rs#L382 // https://github.com/rust-lang/rust/blob/1dea922ea6e74f99a0e97de5cdb8174e4dea0444/compiler/rustc_const_eval/src/transform/check_consts/check.rs#L382
if !bb.is_cleanup { if !bb.is_cleanup {
check_terminator(tcx, body, bb.terminator(), msrv)?; check_terminator(cx, body, bb.terminator(), msrv)?;
for stmt in &bb.statements { for stmt in &bb.statements {
check_statement(tcx, body, def_id, stmt, msrv)?; check_statement(cx, body, def_id, stmt, msrv)?;
} }
} }
} }
Ok(()) Ok(())
} }
fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, msrv: &Msrv) -> McfResult { fn check_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, msrv: Msrv) -> McfResult {
for arg in ty.walk() { for arg in ty.walk() {
let ty = match arg.unpack() { let ty = match arg.unpack() {
GenericArgKind::Type(ty) => ty, GenericArgKind::Type(ty) => ty,
@@ -63,7 +64,7 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, msrv: &Msrv) -> M
}; };
match ty.kind() { match ty.kind() {
ty::Ref(_, _, hir::Mutability::Mut) if !msrv.meets(msrvs::CONST_MUT_REFS) => { ty::Ref(_, _, hir::Mutability::Mut) if !msrv.meets(cx, msrvs::CONST_MUT_REFS) => {
return Err((span, "mutable references in const fn are unstable".into())); return Err((span, "mutable references in const fn are unstable".into()));
}, },
ty::Alias(ty::Opaque, ..) => return Err((span, "`impl Trait` in const fn is unstable".into())), ty::Alias(ty::Opaque, ..) => return Err((span, "`impl Trait` in const fn is unstable".into())),
@@ -82,7 +83,7 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, msrv: &Msrv) -> M
)); ));
}, },
ty::ExistentialPredicate::Trait(trait_ref) => { ty::ExistentialPredicate::Trait(trait_ref) => {
if Some(trait_ref.def_id) != tcx.lang_items().sized_trait() { if Some(trait_ref.def_id) != cx.tcx.lang_items().sized_trait() {
return Err(( return Err((
span, span,
"trait bounds other than `Sized` \ "trait bounds other than `Sized` \
@@ -101,19 +102,19 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span, msrv: &Msrv) -> M
} }
fn check_rvalue<'tcx>( fn check_rvalue<'tcx>(
tcx: TyCtxt<'tcx>, cx: &LateContext<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
def_id: DefId, def_id: DefId,
rvalue: &Rvalue<'tcx>, rvalue: &Rvalue<'tcx>,
span: Span, span: Span,
msrv: &Msrv, msrv: Msrv,
) -> McfResult { ) -> McfResult {
match rvalue { match rvalue {
Rvalue::ThreadLocalRef(_) => Err((span, "cannot access thread local storage in const fn".into())), Rvalue::ThreadLocalRef(_) => Err((span, "cannot access thread local storage in const fn".into())),
Rvalue::Len(place) | Rvalue::Discriminant(place) | Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) => { Rvalue::Len(place) | Rvalue::Discriminant(place) | Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) => {
check_place(tcx, *place, span, body, msrv) check_place(cx, *place, span, body, msrv)
}, },
Rvalue::CopyForDeref(place) => check_place(tcx, *place, span, body, msrv), Rvalue::CopyForDeref(place) => check_place(cx, *place, span, body, msrv),
Rvalue::Repeat(operand, _) Rvalue::Repeat(operand, _)
| Rvalue::Use(operand) | Rvalue::Use(operand)
| Rvalue::WrapUnsafeBinder(operand, _) | Rvalue::WrapUnsafeBinder(operand, _)
@@ -128,7 +129,7 @@ fn check_rvalue<'tcx>(
| CastKind::PointerCoercion(PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer, _), | CastKind::PointerCoercion(PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer, _),
operand, operand,
_, _,
) => check_operand(tcx, operand, span, body, msrv), ) => check_operand(cx, operand, span, body, msrv),
Rvalue::Cast( Rvalue::Cast(
CastKind::PointerCoercion( CastKind::PointerCoercion(
PointerCoercion::UnsafeFnPointer PointerCoercion::UnsafeFnPointer
@@ -144,9 +145,11 @@ fn check_rvalue<'tcx>(
// We cannot allow this for now. // We cannot allow this for now.
return Err((span, "unsizing casts are only allowed for references right now".into())); return Err((span, "unsizing casts are only allowed for references right now".into()));
}; };
let unsized_ty = tcx.struct_tail_for_codegen(pointee_ty, ty::TypingEnv::post_analysis(tcx, def_id)); let unsized_ty = cx
.tcx
.struct_tail_for_codegen(pointee_ty, ty::TypingEnv::post_analysis(cx.tcx, def_id));
if let ty::Slice(_) | ty::Str = unsized_ty.kind() { if let ty::Slice(_) | ty::Str = unsized_ty.kind() {
check_operand(tcx, op, span, body, msrv)?; check_operand(cx, op, span, body, msrv)?;
// Casting/coercing things to slices is fine. // Casting/coercing things to slices is fine.
Ok(()) Ok(())
} else { } else {
@@ -167,9 +170,9 @@ fn check_rvalue<'tcx>(
)), )),
// binops are fine on integers // binops are fine on integers
Rvalue::BinaryOp(_, box (lhs, rhs)) => { Rvalue::BinaryOp(_, box (lhs, rhs)) => {
check_operand(tcx, lhs, span, body, msrv)?; check_operand(cx, lhs, span, body, msrv)?;
check_operand(tcx, rhs, span, body, msrv)?; check_operand(cx, rhs, span, body, msrv)?;
let ty = lhs.ty(body, tcx); let ty = lhs.ty(body, cx.tcx);
if ty.is_integral() || ty.is_bool() || ty.is_char() { if ty.is_integral() || ty.is_bool() || ty.is_char() {
Ok(()) Ok(())
} else { } else {
@@ -185,16 +188,16 @@ fn check_rvalue<'tcx>(
) )
| Rvalue::ShallowInitBox(_, _) => Ok(()), | Rvalue::ShallowInitBox(_, _) => Ok(()),
Rvalue::UnaryOp(_, operand) => { Rvalue::UnaryOp(_, operand) => {
let ty = operand.ty(body, tcx); let ty = operand.ty(body, cx.tcx);
if ty.is_integral() || ty.is_bool() { if ty.is_integral() || ty.is_bool() {
check_operand(tcx, operand, span, body, msrv) check_operand(cx, operand, span, body, msrv)
} else { } else {
Err((span, "only int and `bool` operations are stable in const fn".into())) Err((span, "only int and `bool` operations are stable in const fn".into()))
} }
}, },
Rvalue::Aggregate(_, operands) => { Rvalue::Aggregate(_, operands) => {
for operand in operands { for operand in operands {
check_operand(tcx, operand, span, body, msrv)?; check_operand(cx, operand, span, body, msrv)?;
} }
Ok(()) Ok(())
}, },
@@ -202,33 +205,33 @@ fn check_rvalue<'tcx>(
} }
fn check_statement<'tcx>( fn check_statement<'tcx>(
tcx: TyCtxt<'tcx>, cx: &LateContext<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
def_id: DefId, def_id: DefId,
statement: &Statement<'tcx>, statement: &Statement<'tcx>,
msrv: &Msrv, msrv: Msrv,
) -> McfResult { ) -> McfResult {
let span = statement.source_info.span; let span = statement.source_info.span;
match &statement.kind { match &statement.kind {
StatementKind::Assign(box (place, rval)) => { StatementKind::Assign(box (place, rval)) => {
check_place(tcx, *place, span, body, msrv)?; check_place(cx, *place, span, body, msrv)?;
check_rvalue(tcx, body, def_id, rval, span, msrv) check_rvalue(cx, body, def_id, rval, span, msrv)
}, },
StatementKind::FakeRead(box (_, place)) => check_place(tcx, *place, span, body, msrv), StatementKind::FakeRead(box (_, place)) => check_place(cx, *place, span, body, msrv),
// just an assignment // just an assignment
StatementKind::SetDiscriminant { place, .. } | StatementKind::Deinit(place) => { StatementKind::SetDiscriminant { place, .. } | StatementKind::Deinit(place) => {
check_place(tcx, **place, span, body, msrv) check_place(cx, **place, span, body, msrv)
}, },
StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => check_operand(tcx, op, span, body, msrv), StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => check_operand(cx, op, span, body, msrv),
StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping( StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(
rustc_middle::mir::CopyNonOverlapping { dst, src, count }, rustc_middle::mir::CopyNonOverlapping { dst, src, count },
)) => { )) => {
check_operand(tcx, dst, span, body, msrv)?; check_operand(cx, dst, span, body, msrv)?;
check_operand(tcx, src, span, body, msrv)?; check_operand(cx, src, span, body, msrv)?;
check_operand(tcx, count, span, body, msrv) check_operand(cx, count, span, body, msrv)
}, },
// These are all NOPs // These are all NOPs
StatementKind::StorageLive(_) StatementKind::StorageLive(_)
@@ -244,16 +247,16 @@ fn check_statement<'tcx>(
} }
fn check_operand<'tcx>( fn check_operand<'tcx>(
tcx: TyCtxt<'tcx>, cx: &LateContext<'tcx>,
operand: &Operand<'tcx>, operand: &Operand<'tcx>,
span: Span, span: Span,
body: &Body<'tcx>, body: &Body<'tcx>,
msrv: &Msrv, msrv: Msrv,
) -> McfResult { ) -> McfResult {
match operand { match operand {
Operand::Move(place) => { Operand::Move(place) => {
if !place.projection.as_ref().is_empty() if !place.projection.as_ref().is_empty()
&& !is_ty_const_destruct(tcx, place.ty(&body.local_decls, tcx).ty, body) && !is_ty_const_destruct(cx.tcx, place.ty(&body.local_decls, cx.tcx).ty, body)
{ {
return Err(( return Err((
span, span,
@@ -261,29 +264,35 @@ fn check_operand<'tcx>(
)); ));
} }
check_place(tcx, *place, span, body, msrv) check_place(cx, *place, span, body, msrv)
}, },
Operand::Copy(place) => check_place(tcx, *place, span, body, msrv), Operand::Copy(place) => check_place(cx, *place, span, body, msrv),
Operand::Constant(c) => match c.check_static_ptr(tcx) { Operand::Constant(c) => match c.check_static_ptr(cx.tcx) {
Some(_) => Err((span, "cannot access `static` items in const fn".into())), Some(_) => Err((span, "cannot access `static` items in const fn".into())),
None => Ok(()), None => Ok(()),
}, },
} }
} }
fn check_place<'tcx>(tcx: TyCtxt<'tcx>, place: Place<'tcx>, span: Span, body: &Body<'tcx>, msrv: &Msrv) -> McfResult { fn check_place<'tcx>(
cx: &LateContext<'tcx>,
place: Place<'tcx>,
span: Span,
body: &Body<'tcx>,
msrv: Msrv,
) -> McfResult {
for (base, elem) in place.as_ref().iter_projections() { for (base, elem) in place.as_ref().iter_projections() {
match elem { match elem {
ProjectionElem::Field(..) => { ProjectionElem::Field(..) => {
if base.ty(body, tcx).ty.is_union() && !msrv.meets(msrvs::CONST_FN_UNION) { if base.ty(body, cx.tcx).ty.is_union() && !msrv.meets(cx, msrvs::CONST_FN_UNION) {
return Err((span, "accessing union fields is unstable".into())); return Err((span, "accessing union fields is unstable".into()));
} }
}, },
ProjectionElem::Deref => match base.ty(body, tcx).ty.kind() { ProjectionElem::Deref => match base.ty(body, cx.tcx).ty.kind() {
ty::RawPtr(_, hir::Mutability::Mut) => { ty::RawPtr(_, hir::Mutability::Mut) => {
return Err((span, "dereferencing raw mut pointer in const fn is unstable".into())); return Err((span, "dereferencing raw mut pointer in const fn is unstable".into()));
}, },
ty::RawPtr(_, hir::Mutability::Not) if !msrv.meets(msrvs::CONST_RAW_PTR_DEREF) => { ty::RawPtr(_, hir::Mutability::Not) if !msrv.meets(cx, msrvs::CONST_RAW_PTR_DEREF) => {
return Err((span, "dereferencing raw const pointer in const fn is unstable".into())); return Err((span, "dereferencing raw const pointer in const fn is unstable".into()));
}, },
_ => (), _ => (),
@@ -302,10 +311,10 @@ fn check_place<'tcx>(tcx: TyCtxt<'tcx>, place: Place<'tcx>, span: Span, body: &B
} }
fn check_terminator<'tcx>( fn check_terminator<'tcx>(
tcx: TyCtxt<'tcx>, cx: &LateContext<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
terminator: &Terminator<'tcx>, terminator: &Terminator<'tcx>,
msrv: &Msrv, msrv: Msrv,
) -> McfResult { ) -> McfResult {
let span = terminator.source_info.span; let span = terminator.source_info.span;
match &terminator.kind { match &terminator.kind {
@@ -317,7 +326,7 @@ fn check_terminator<'tcx>(
| TerminatorKind::UnwindTerminate(_) | TerminatorKind::UnwindTerminate(_)
| TerminatorKind::Unreachable => Ok(()), | TerminatorKind::Unreachable => Ok(()),
TerminatorKind::Drop { place, .. } => { TerminatorKind::Drop { place, .. } => {
if !is_ty_const_destruct(tcx, place.ty(&body.local_decls, tcx).ty, body) { if !is_ty_const_destruct(cx.tcx, place.ty(&body.local_decls, cx.tcx).ty, body) {
return Err(( return Err((
span, span,
"cannot drop locals with a non constant destructor in const fn".into(), "cannot drop locals with a non constant destructor in const fn".into(),
@@ -325,7 +334,7 @@ fn check_terminator<'tcx>(
} }
Ok(()) Ok(())
}, },
TerminatorKind::SwitchInt { discr, targets: _ } => check_operand(tcx, discr, span, body, msrv), TerminatorKind::SwitchInt { discr, targets: _ } => check_operand(cx, discr, span, body, msrv),
TerminatorKind::CoroutineDrop | TerminatorKind::Yield { .. } => { TerminatorKind::CoroutineDrop | TerminatorKind::Yield { .. } => {
Err((span, "const fn coroutines are unstable".into())) Err((span, "const fn coroutines are unstable".into()))
}, },
@@ -339,9 +348,9 @@ fn check_terminator<'tcx>(
fn_span: _, fn_span: _,
} }
| TerminatorKind::TailCall { func, args, fn_span: _ } => { | TerminatorKind::TailCall { func, args, fn_span: _ } => {
let fn_ty = func.ty(body, tcx); let fn_ty = func.ty(body, cx.tcx);
if let ty::FnDef(fn_def_id, _) = *fn_ty.kind() { if let ty::FnDef(fn_def_id, _) = *fn_ty.kind() {
if !is_stable_const_fn(tcx, fn_def_id, msrv) { if !is_stable_const_fn(cx, fn_def_id, msrv) {
return Err(( return Err((
span, span,
format!( format!(
@@ -356,17 +365,17 @@ fn check_terminator<'tcx>(
// within const fns. `transmute` is allowed in all other const contexts. // within const fns. `transmute` is allowed in all other const contexts.
// This won't really scale to more intrinsics or functions. Let's allow const // This won't really scale to more intrinsics or functions. Let's allow const
// transmutes in const fn before we add more hacks to this. // transmutes in const fn before we add more hacks to this.
if tcx.is_intrinsic(fn_def_id, sym::transmute) { if cx.tcx.is_intrinsic(fn_def_id, sym::transmute) {
return Err(( return Err((
span, span,
"can only call `transmute` from const items, not `const fn`".into(), "can only call `transmute` from const items, not `const fn`".into(),
)); ));
} }
check_operand(tcx, func, span, body, msrv)?; check_operand(cx, func, span, body, msrv)?;
for arg in args { for arg in args {
check_operand(tcx, &arg.node, span, body, msrv)?; check_operand(cx, &arg.node, span, body, msrv)?;
} }
Ok(()) Ok(())
} else { } else {
@@ -379,14 +388,14 @@ fn check_terminator<'tcx>(
msg: _, msg: _,
target: _, target: _,
unwind: _, unwind: _,
} => check_operand(tcx, cond, span, body, msrv), } => check_operand(cx, cond, span, body, msrv),
TerminatorKind::InlineAsm { .. } => Err((span, "cannot use inline assembly in const fn".into())), TerminatorKind::InlineAsm { .. } => Err((span, "cannot use inline assembly in const fn".into())),
} }
} }
fn is_stable_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool { fn is_stable_const_fn(cx: &LateContext<'_>, def_id: DefId, msrv: Msrv) -> bool {
tcx.is_const_fn(def_id) cx.tcx.is_const_fn(def_id)
&& tcx.lookup_const_stability(def_id).is_none_or(|const_stab| { && cx.tcx.lookup_const_stability(def_id).is_none_or(|const_stab| {
if let rustc_attr_parsing::StabilityLevel::Stable { since, .. } = const_stab.level { if let rustc_attr_parsing::StabilityLevel::Stable { since, .. } = const_stab.level {
// Checking MSRV is manually necessary because `rustc` has no such concept. This entire // Checking MSRV is manually necessary because `rustc` has no such concept. This entire
// function could be removed if `rustc` provided a MSRV-aware version of `is_stable_const_fn`. // function could be removed if `rustc` provided a MSRV-aware version of `is_stable_const_fn`.
@@ -398,10 +407,10 @@ fn is_stable_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool {
StableSince::Err => return false, StableSince::Err => return false,
}; };
msrv.meets(const_stab_rust_version) msrv.meets(cx, const_stab_rust_version)
} else { } else {
// Unstable const fn, check if the feature is enabled. // Unstable const fn, check if the feature is enabled.
tcx.features().enabled(const_stab.feature) && msrv.current().is_none() cx.tcx.features().enabled(const_stab.feature) && msrv.current(cx).is_none()
} }
}) })
} }

View File

@@ -9,7 +9,7 @@ extern crate rustc_middle;
#[macro_use] #[macro_use]
extern crate rustc_session; extern crate rustc_session;
use clippy_utils::extract_msrv_attr; use clippy_utils::extract_msrv_attr;
use clippy_utils::msrvs::Msrv; use clippy_utils::msrvs::MsrvStack;
use rustc_hir::Expr; use rustc_hir::Expr;
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass}; use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
@@ -20,18 +20,13 @@ declare_lint! {
} }
struct Pass { struct Pass {
msrv: Msrv, msrv: MsrvStack,
} }
impl_lint_pass!(Pass => [TEST_LINT]); impl_lint_pass!(Pass => [TEST_LINT]);
impl LateLintPass<'_> for Pass {
extract_msrv_attr!(LateContext);
fn check_expr(&mut self, _: &LateContext<'_>, _: &Expr<'_>) {}
}
impl EarlyLintPass for Pass { impl EarlyLintPass for Pass {
extract_msrv_attr!(EarlyContext); extract_msrv_attr!();
fn check_expr(&mut self, _: &EarlyContext<'_>, _: &rustc_ast::Expr) {} fn check_expr(&mut self, _: &EarlyContext<'_>, _: &rustc_ast::Expr) {}
} }

View File

@@ -9,7 +9,7 @@ extern crate rustc_middle;
#[macro_use] #[macro_use]
extern crate rustc_session; extern crate rustc_session;
use clippy_utils::extract_msrv_attr; use clippy_utils::extract_msrv_attr;
use clippy_utils::msrvs::Msrv; use clippy_utils::msrvs::MsrvStack;
use rustc_hir::Expr; use rustc_hir::Expr;
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass}; use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
@@ -20,15 +20,11 @@ declare_lint! {
} }
struct Pass { struct Pass {
msrv: Msrv, msrv: MsrvStack,
} }
impl_lint_pass!(Pass => [TEST_LINT]); impl_lint_pass!(Pass => [TEST_LINT]);
impl LateLintPass<'_> for Pass {
fn check_expr(&mut self, _: &LateContext<'_>, _: &Expr<'_>) {}
}
impl EarlyLintPass for Pass { impl EarlyLintPass for Pass {
fn check_expr(&mut self, _: &EarlyContext<'_>, _: &rustc_ast::Expr) {} fn check_expr(&mut self, _: &EarlyContext<'_>, _: &rustc_ast::Expr) {}
} }

View File

@@ -1,8 +1,8 @@
error: `extract_msrv_attr!` macro missing from `LateLintPass` implementation error: `extract_msrv_attr!` macro missing from `EarlyLintPass` implementation
--> tests/ui-internal/invalid_msrv_attr_impl.rs:28:1 --> tests/ui-internal/invalid_msrv_attr_impl.rs:28:1
| |
LL | impl LateLintPass<'_> for Pass { LL | impl EarlyLintPass for Pass {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
note: the lint level is defined here note: the lint level is defined here
--> tests/ui-internal/invalid_msrv_attr_impl.rs:1:9 --> tests/ui-internal/invalid_msrv_attr_impl.rs:1:9
@@ -10,23 +10,11 @@ note: the lint level is defined here
LL | #![deny(clippy::internal)] LL | #![deny(clippy::internal)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
= note: `#[deny(clippy::missing_msrv_attr_impl)]` implied by `#[deny(clippy::internal)]` = note: `#[deny(clippy::missing_msrv_attr_impl)]` implied by `#[deny(clippy::internal)]`
help: add `extract_msrv_attr!(LateContext)` to the `LateLintPass` implementation help: add `extract_msrv_attr!()` to the `EarlyLintPass` implementation
| |
LL ~ impl LateLintPass<'_> for Pass { LL + impl EarlyLintPass for Pass {
LL + extract_msrv_attr!(LateContext); LL + extract_msrv_attr!();
| |
error: `extract_msrv_attr!` macro missing from `EarlyLintPass` implementation error: aborting due to 1 previous error
--> tests/ui-internal/invalid_msrv_attr_impl.rs:32:1
|
LL | impl EarlyLintPass for Pass {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: add `extract_msrv_attr!(EarlyContext)` to the `EarlyLintPass` implementation
|
LL ~ impl EarlyLintPass for Pass {
LL + extract_msrv_attr!(EarlyContext);
|
error: aborting due to 2 previous errors

View File

@@ -0,0 +1,12 @@
#![allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]
#![forbid(clippy::ptr_as_ptr)]
/// MSRV checking in late passes skips checking the parent nodes if no early pass sees a
/// `#[clippy::msrv]` attribute
///
/// Here we ensure that even if all early passes are allowed (above) the attribute is still detected
/// in late lints such as `clippy::ptr_as_ptr`
#[clippy::msrv = "1.37"]
fn f(p: *const i32) {
let _ = p as *const i64;
}