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 Jan 18, 2025
1 parent 77ee982 commit 57dae04
Show file tree
Hide file tree
Showing 89 changed files with 356 additions and 475 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
6 changes: 2 additions & 4 deletions clippy_lints/src/approx_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct ApproxConstant {
impl ApproxConstant {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
msrv: conf.msrv,
}
}

Expand All @@ -92,7 +92,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 @@ -116,8 +116,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
6 changes: 2 additions & 4 deletions clippy_lints/src/assigning_clones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct AssigningClones {
impl AssigningClones {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
msrv: conf.msrv,
}
}
}
Expand Down Expand Up @@ -90,7 +90,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 +143,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
20 changes: 9 additions & 11 deletions clippy_lints/src/attrs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod useless_attribute;
mod utils;

use clippy_config::Conf;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::msrvs::{self, Msrv, MsrvStack};
use rustc_ast::{self as ast, Attribute, MetaItemInner, MetaItemKind};
use rustc_hir::{ImplItem, Item, TraitItem};
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
Expand Down Expand Up @@ -460,7 +460,7 @@ impl_lint_pass!(Attributes => [
impl Attributes {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
msrv: conf.msrv,
}
}
}
Expand All @@ -471,7 +471,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
if is_relevant_item(cx, item) {
inline_always::check(cx, item.span, item.ident.name, attrs);
}
repr_attributes::check(cx, item.span, attrs, &self.msrv);
repr_attributes::check(cx, item.span, attrs, self.msrv);
}

fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
Expand All @@ -485,18 +485,16 @@ 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,
msrv: MsrvStack,
}

impl EarlyAttributes {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
msrv: MsrvStack::new(conf.msrv),
}
}
}
Expand All @@ -515,17 +513,17 @@ impl EarlyLintPass for EarlyAttributes {
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 @@ -589,5 +587,5 @@ impl EarlyLintPass for PostExpansionEarlyAttributes {
duplicated_attributes::check(cx, &item.attrs);
}

extract_msrv_attr!(EarlyContext);
extract_msrv_attr!();
}
11 changes: 3 additions & 8 deletions clippy_lints/src/attrs/repr_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ use rustc_lint::LateContext;
use rustc_span::{Span, sym};

use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::msrvs;
use clippy_utils::msrvs::{self, Msrv};

use super::REPR_PACKED_WITHOUT_ABI;

pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute], msrv: &msrvs::Msrv) {
if msrv.meets(msrvs::REPR_RUST) {
check_packed(cx, item_span, attrs);
}
}

fn check_packed(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute]) {
pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute], msrv: Msrv) {
if let Some(items) = attrs.iter().find_map(|attr| {
if attr.ident().is_some_and(|ident| matches!(ident.name, sym::repr)) {
attr.meta_item_list()
Expand All @@ -27,6 +21,7 @@ fn check_packed(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute]) {
item.ident()
.is_some_and(|ident| matches!(ident.name, sym::C | sym::Rust))
})
&& msrv.meets(cx, msrvs::REPR_RUST)
{
span_lint_and_then(
cx,
Expand Down
20 changes: 10 additions & 10 deletions clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub struct NonminimalBool {
impl NonminimalBool {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
msrv: conf.msrv,
}
}
}
Expand All @@ -102,7 +102,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 @@ -119,8 +119,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 @@ -197,7 +195,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 @@ -233,7 +231,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 @@ -326,7 +324,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 @@ -396,7 +394,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 @@ -438,7 +436,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, _)| {
a == path.ident.name.as_str() && msrv.is_none_or(|msrv| curr_msrv.meets(cx, msrv))
})
.and_then(|(_, _, neg_method)| {
let negated_args = args
.iter()
Expand Down Expand Up @@ -467,7 +467,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 @@ -16,7 +16,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 @@ -34,7 +34,7 @@ pub(super) fn check<'tcx>(
return false;
}

let (suggestion, span) = if msrv.meets(msrvs::RAW_REF_OP) {
let (suggestion, span) = if msrv.meets(cx, msrvs::RAW_REF_OP) {
let operator_kind = match mutability {
Mutability::Not => "const",
Mutability::Mut => "mut",
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/casts/cast_abs_to_unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub(super) fn check(
cast_expr: &Expr<'_>,
cast_from: Ty<'_>,
cast_to: Ty<'_>,
msrv: &Msrv,
msrv: Msrv,
) {
if msrv.meets(msrvs::UNSIGNED_ABS)
&& let ty::Int(from) = cast_from.kind()
if let ty::Int(from) = cast_from.kind()
&& let ty::Uint(to) = cast_to.kind()
&& let ExprKind::MethodCall(method_path, receiver, [], _) = cast_expr.kind
&& method_path.ident.name.as_str() == "abs"
&& msrv.meets(cx, msrvs::UNSIGNED_ABS)
{
let span = if from.bit_width() == to.bit_width() {
expr.span
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
9 changes: 2 additions & 7 deletions clippy_lints/src/casts/cast_slice_different_sizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ 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) {
// suggestion is invalid if `ptr::slice_from_raw_parts` does not exist
if !msrv.meets(msrvs::PTR_SLICE_RAW_PARTS) {
return;
}

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv) {
// if this cast is the child of another cast expression then don't emit something for it, the full
// chain will be analyzed
if is_child_of_cast(cx, expr) {
Expand All @@ -30,7 +25,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Msrv
if let (Ok(from_layout), Ok(to_layout)) = (cx.layout_of(start_ty.ty), cx.layout_of(end_ty.ty)) {
let from_size = from_layout.size.bytes();
let to_size = to_layout.size.bytes();
if from_size != to_size && from_size != 0 && to_size != 0 {
if from_size != to_size && from_size != 0 && to_size != 0 && msrv.meets(cx, msrvs::PTR_SLICE_RAW_PARTS) {
span_lint_and_then(
cx,
CAST_SLICE_DIFFERENT_SIZES,
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/casts/cast_slice_from_raw_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ 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)
&& let ty::RawPtr(ptrty, _) = cast_to.kind()
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>, msrv: Msrv) {
if 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
&& let ExprKind::Path(ref qpath) = fun.kind
&& let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id()
&& let Some(rpk) = raw_parts_kind(cx, fun_def_id)
&& let ctxt = expr.span.ctxt()
&& cast_expr.span.ctxt() == ctxt
&& msrv.meets(cx, msrvs::PTR_SLICE_RAW_PARTS)
{
let func = match rpk {
RawPartsKind::Immutable => "from_raw_parts",
Expand Down
Loading

0 comments on commit 57dae04

Please sign in to comment.