Skip to content

Commit

Permalink
Migrate clippy_lints to new MSRV API
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexendoo committed Dec 13, 2024
1 parent 72c69e1 commit 507e6f0
Show file tree
Hide file tree
Showing 85 changed files with 310 additions and 461 deletions.
8 changes: 4 additions & 4 deletions clippy_lints/src/almost_complete_range.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::msrvs::{self, MsrvStack};
use clippy_utils::source::{trim_span, walk_span_to_context};
use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits};
use rustc_errors::Applicability;
Expand Down Expand Up @@ -32,12 +32,12 @@ declare_clippy_lint! {
impl_lint_pass!(AlmostCompleteRange => [ALMOST_COMPLETE_RANGE]);

pub struct AlmostCompleteRange {
msrv: Msrv,
msrv: MsrvStack,
}
impl AlmostCompleteRange {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
msrv: MsrvStack::new(conf.msrv),
}
}
}
Expand Down Expand Up @@ -97,7 +97,7 @@ impl EarlyLintPass for AlmostCompleteRange {
}
}

extract_msrv_attr!(EarlyContext);
extract_msrv_attr!();
}

fn is_incomplete_range(start: &Expr, end: &Expr) -> bool {
Expand Down
8 changes: 2 additions & 6 deletions clippy_lints/src/approx_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ pub struct ApproxConstant {

impl ApproxConstant {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
}
Self { msrv: conf.msrv }
}

fn check_lit(&self, cx: &LateContext<'_>, lit: &LitKind, e: &Expr<'_>) {
Expand All @@ -91,7 +89,7 @@ impl ApproxConstant {
let s = s.as_str();
if s.parse::<f64>().is_ok() {
for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
if is_approx_const(constant, s, min_digits) && msrv.is_none_or(|msrv| self.msrv.meets(msrv)) {
if is_approx_const(constant, s, min_digits) && msrv.is_none_or(|msrv| self.msrv.meets(cx, msrv)) {
span_lint_and_help(
cx,
APPROX_CONSTANT,
Expand All @@ -115,8 +113,6 @@ impl<'tcx> LateLintPass<'tcx> for ApproxConstant {
self.check_lit(cx, &lit.node, e);
}
}

extract_msrv_attr!(LateContext);
}

/// Returns `false` if the number of significant figures in `value` are
Expand Down
8 changes: 2 additions & 6 deletions clippy_lints/src/assigning_clones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ pub struct AssigningClones {

impl AssigningClones {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
}
Self { msrv: conf.msrv }
}
}

Expand Down Expand Up @@ -90,7 +88,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
sym::clone if is_diag_trait_item(cx, fn_id, sym::Clone) => CloneTrait::Clone,
_ if fn_name.as_str() == "to_owned"
&& is_diag_trait_item(cx, fn_id, sym::ToOwned)
&& self.msrv.meets(msrvs::CLONE_INTO) =>
&& self.msrv.meets(cx, msrvs::CLONE_INTO) =>
{
CloneTrait::ToOwned
},
Expand Down Expand Up @@ -143,8 +141,6 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
);
}
}

extract_msrv_attr!(LateContext);
}

/// Checks if the data being cloned borrows from the place that is being assigned to:
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/attrs/deprecated_cfg_attr.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::{Attribute, DEPRECATED_CFG_ATTR, DEPRECATED_CLIPPY_CFG_ATTR, unnecessary_clippy_cfg};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::msrvs::{self, MsrvStack};
use rustc_ast::AttrStyle;
use rustc_errors::Applicability;
use rustc_lint::EarlyContext;
use rustc_span::sym;

pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msrv) {
pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &MsrvStack) {
// check cfg_attr
if attr.has_name(sym::cfg_attr)
&& let Some(items) = attr.meta_item_list()
Expand Down
40 changes: 13 additions & 27 deletions clippy_lints/src/attrs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ mod useless_attribute;
mod utils;

use clippy_config::Conf;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::msrvs::{self, MsrvStack};
use rustc_ast::{self as ast, Attribute, MetaItemInner, MetaItemKind};
use rustc_hir::{ImplItem, Item, TraitItem};
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
use rustc_session::impl_lint_pass;
use rustc_session::{declare_lint_pass, impl_lint_pass};
use rustc_span::sym;
use utils::{is_lint_level, is_relevant_impl, is_relevant_item, is_relevant_trait};

Expand Down Expand Up @@ -409,22 +409,10 @@ declare_clippy_lint! {
"duplicated attribute"
}

pub struct Attributes {
msrv: Msrv,
}

impl_lint_pass!(Attributes => [
declare_lint_pass!(Attributes => [
INLINE_ALWAYS,
]);

impl Attributes {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
}
}
}

impl<'tcx> LateLintPass<'tcx> for Attributes {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
let attrs = cx.tcx.hir().attrs(item.hir_id());
Expand All @@ -444,47 +432,45 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
inline_always::check(cx, item.span, item.ident.name, cx.tcx.hir().attrs(item.hir_id()));
}
}

extract_msrv_attr!(LateContext);
}

pub struct EarlyAttributes {
msrv: Msrv,
pub struct PreExpansionEarlyAttributes {
msrv: MsrvStack,
}

impl EarlyAttributes {
impl PreExpansionEarlyAttributes {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
msrv: MsrvStack::new(conf.msrv),
}
}
}

impl_lint_pass!(EarlyAttributes => [
impl_lint_pass!(PreExpansionEarlyAttributes => [
DEPRECATED_CFG_ATTR,
NON_MINIMAL_CFG,
DEPRECATED_CLIPPY_CFG_ATTR,
UNNECESSARY_CLIPPY_CFG,
]);

impl EarlyLintPass for EarlyAttributes {
impl EarlyLintPass for PreExpansionEarlyAttributes {
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
deprecated_cfg_attr::check(cx, attr, &self.msrv);
deprecated_cfg_attr::check_clippy(cx, attr);
non_minimal_cfg::check(cx, attr);
}

extract_msrv_attr!(EarlyContext);
extract_msrv_attr!();
}

pub struct PostExpansionEarlyAttributes {
msrv: Msrv,
msrv: MsrvStack,
}

impl PostExpansionEarlyAttributes {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
msrv: MsrvStack::new(conf.msrv),
}
}
}
Expand Down Expand Up @@ -548,5 +534,5 @@ impl EarlyLintPass for PostExpansionEarlyAttributes {
duplicated_attributes::check(cx, &item.attrs);
}

extract_msrv_attr!(EarlyContext);
extract_msrv_attr!();
}
22 changes: 10 additions & 12 deletions clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ pub struct NonminimalBool {

impl NonminimalBool {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
}
Self { msrv: conf.msrv }
}
}

Expand All @@ -101,7 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
_: Span,
_: LocalDefId,
) {
NonminimalBoolVisitor { cx, msrv: &self.msrv }.visit_body(body);
NonminimalBoolVisitor { cx, msrv: self.msrv }.visit_body(body);
}

fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
Expand All @@ -118,8 +116,6 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
_ => {},
}
}

extract_msrv_attr!(LateContext);
}

fn inverted_bin_op_eq_str(op: BinOpKind) -> Option<&'static str> {
Expand Down Expand Up @@ -196,7 +192,7 @@ fn check_inverted_bool_in_condition(
);
}

fn check_simplify_not(cx: &LateContext<'_>, msrv: &Msrv, expr: &Expr<'_>) {
fn check_simplify_not(cx: &LateContext<'_>, msrv: Msrv, expr: &Expr<'_>) {
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind
&& !expr.span.from_expansion()
&& !inner.span.from_expansion()
Expand Down Expand Up @@ -232,7 +228,7 @@ fn check_simplify_not(cx: &LateContext<'_>, msrv: &Msrv, expr: &Expr<'_>) {

struct NonminimalBoolVisitor<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
msrv: &'a Msrv,
msrv: Msrv,
}

use quine_mc_cluskey::Bool;
Expand Down Expand Up @@ -325,7 +321,7 @@ impl<'v> Hir2Qmm<'_, '_, 'v> {
struct SuggestContext<'a, 'tcx, 'v> {
terminals: &'v [&'v Expr<'v>],
cx: &'a LateContext<'tcx>,
msrv: &'a Msrv,
msrv: Msrv,
output: String,
}

Expand Down Expand Up @@ -395,7 +391,7 @@ impl SuggestContext<'_, '_, '_> {
}
}

fn simplify_not(cx: &LateContext<'_>, curr_msrv: &Msrv, expr: &Expr<'_>) -> Option<String> {
fn simplify_not(cx: &LateContext<'_>, curr_msrv: Msrv, expr: &Expr<'_>) -> Option<String> {
match &expr.kind {
ExprKind::Binary(binop, lhs, rhs) => {
if !implements_ord(cx, lhs) {
Expand Down Expand Up @@ -437,7 +433,9 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: &Msrv, expr: &Expr<'_>) -> Opti
.iter()
.copied()
.flat_map(|(msrv, a, b)| vec![(msrv, a, b), (msrv, b, a)])
.find(|&(msrv, a, _)| msrv.is_none_or(|msrv| curr_msrv.meets(msrv)) && a == path.ident.name.as_str())
.find(|&(msrv, a, _)| {
msrv.is_none_or(|msrv| curr_msrv.meets(cx, msrv)) && a == path.ident.name.as_str()
})
.and_then(|(_, _, neg_method)| {
let negated_args = args
.iter()
Expand Down Expand Up @@ -466,7 +464,7 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: &Msrv, expr: &Expr<'_>) -> Opti
}
}

fn suggest(cx: &LateContext<'_>, msrv: &Msrv, suggestion: &Bool, terminals: &[&Expr<'_>]) -> String {
fn suggest(cx: &LateContext<'_>, msrv: Msrv, suggestion: &Bool, terminals: &[&Expr<'_>]) -> String {
let mut suggest_context = SuggestContext {
terminals,
cx,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/casts/borrow_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
expr: &'tcx Expr<'_>,
cast_expr: &'tcx Expr<'_>,
cast_to: &'tcx Ty<'_>,
msrv: &Msrv,
msrv: Msrv,
) -> bool {
if matches!(cast_to.kind, TyKind::Ptr(_))
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
Expand All @@ -32,7 +32,7 @@ pub(super) fn check<'tcx>(
return false;
}

let suggestion = if msrv.meets(msrvs::RAW_REF_OP) {
let suggestion = if msrv.meets(cx, msrvs::RAW_REF_OP) {
let operator_kind = match mutability {
Mutability::Not => "const",
Mutability::Mut => "mut",
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/casts/cast_abs_to_unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub(super) fn check(
cast_expr: &Expr<'_>,
cast_from: Ty<'_>,
cast_to: Ty<'_>,
msrv: &Msrv,
msrv: Msrv,
) {
if msrv.meets(msrvs::UNSIGNED_ABS)
if msrv.meets(cx, msrvs::UNSIGNED_ABS)
&& let ty::Int(from) = cast_from.kind()
&& let ty::Uint(to) = cast_to.kind()
&& let ExprKind::MethodCall(method_path, receiver, [], _) = cast_expr.kind
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/casts/cast_lossless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(super) fn check(
cast_from: Ty<'_>,
cast_to: Ty<'_>,
cast_to_hir: &rustc_hir::Ty<'_>,
msrv: &Msrv,
msrv: Msrv,
) {
if !should_lint(cx, cast_from, cast_to, msrv) {
return;
Expand Down Expand Up @@ -70,7 +70,7 @@ pub(super) fn check(
);
}

fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool {
fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: Msrv) -> bool {
// Do not suggest using From in consts/statics until it is valid to do so (see #2267).
if is_in_const_context(cx) {
return false;
Expand All @@ -96,7 +96,7 @@ fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &
};
!is_isize_or_usize(cast_from) && from_nbits < to_nbits
},
(false, true) if matches!(cast_from.kind(), ty::Bool) && msrv.meets(msrvs::FROM_BOOL) => true,
(false, true) if matches!(cast_from.kind(), ty::Bool) && msrv.meets(cx, msrvs::FROM_BOOL) => true,
(_, _) => {
matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64))
},
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/casts/cast_slice_different_sizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use rustc_middle::ty::{self, Ty, TypeAndMut};

use super::CAST_SLICE_DIFFERENT_SIZES;

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Msrv) {
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv) {
// suggestion is invalid if `ptr::slice_from_raw_parts` does not exist
if !msrv.meets(msrvs::PTR_SLICE_RAW_PARTS) {
if !msrv.meets(cx, msrvs::PTR_SLICE_RAW_PARTS) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/casts/cast_slice_from_raw_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fn raw_parts_kind(cx: &LateContext<'_>, did: DefId) -> Option<RawPartsKind> {
}
}

pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>, msrv: &Msrv) {
if msrv.meets(msrvs::PTR_SLICE_RAW_PARTS)
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>, msrv: Msrv) {
if msrv.meets(cx, msrvs::PTR_SLICE_RAW_PARTS)
&& let ty::RawPtr(ptrty, _) = cast_to.kind()
&& let ty::Slice(_) = ptrty.kind()
&& let ExprKind::Call(fun, [ptr_arg, len_arg]) = cast_expr.peel_blocks().kind
Expand Down
Loading

0 comments on commit 507e6f0

Please sign in to comment.