Skip to content

Commit

Permalink
add MSRV to more lints specified in rust-lang#6097
Browse files Browse the repository at this point in the history
update tests
  • Loading branch information
suyashb95 committed Dec 5, 2020
1 parent 8d678bc commit 66d3509
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 19 deletions.
27 changes: 24 additions & 3 deletions clippy_lints/src/checked_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ use rustc_errors::Applicability;
use rustc_hir::{BinOp, BinOpKind, Expr, ExprKind, QPath, TyKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_semver::RustcVersion;
use rustc_session::{impl_lint_pass, declare_tool_lint};

use crate::utils::{snippet_with_applicability, span_lint_and_sugg, SpanlessEq};
use crate::utils::{meets_msrv, snippet_with_applicability, span_lint_and_sugg, SpanlessEq};

const CHECKED_CONVERSIONS_MSRV: RustcVersion = RustcVersion::new(1, 34, 0);

declare_clippy_lint! {
/// **What it does:** Checks for explicit bounds checking when casting.
Expand Down Expand Up @@ -39,10 +42,26 @@ declare_clippy_lint! {
"`try_from` could replace manual bounds checking when casting"
}

declare_lint_pass!(CheckedConversions => [CHECKED_CONVERSIONS]);
pub struct CheckedConversions {
msrv: Option<RustcVersion>,
}

impl CheckedConversions {
#[must_use]
pub fn new(msrv: Option<RustcVersion>) -> Self {
Self { msrv }
}
}

impl_lint_pass!(CheckedConversions => [CHECKED_CONVERSIONS]);

impl<'tcx> LateLintPass<'tcx> for CheckedConversions {
fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) {

if !meets_msrv(self.msrv.as_ref(), &CHECKED_CONVERSIONS_MSRV) {
return;
}

let result = if_chain! {
if !in_external_macro(cx.sess(), item.span);
if let ExprKind::Binary(op, ref left, ref right) = &item.kind;
Expand Down Expand Up @@ -74,6 +93,8 @@ impl<'tcx> LateLintPass<'tcx> for CheckedConversions {
}
}
}

extract_msrv_attr!(LateContext);
}

/// Searches for a single check from unsigned to _ is done
Expand Down
7 changes: 4 additions & 3 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(move || box matches::Matches::new(msrv));
store.register_early_pass(move || box manual_non_exhaustive::ManualNonExhaustive::new(msrv));
store.register_late_pass(move || box manual_strip::ManualStrip::new(msrv));
store.register_early_pass(move || box redundant_static_lifetimes::RedundantStaticLifetimes::new(msrv));
store.register_early_pass(move || box redundant_field_names::RedundantFieldNames::new(msrv));
store.register_late_pass(move || box checked_conversions::CheckedConversions::new(msrv));

store.register_late_pass(|| box size_of_in_element_count::SizeOfInElementCount);
store.register_late_pass(|| box map_clone::MapClone);
store.register_late_pass(|| box map_err_ignore::MapErrIgnore);
Expand Down Expand Up @@ -1106,7 +1110,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box missing_const_for_fn::MissingConstForFn);
store.register_late_pass(|| box transmuting_null::TransmutingNull);
store.register_late_pass(|| box path_buf_push_overwrite::PathBufPushOverwrite);
store.register_late_pass(|| box checked_conversions::CheckedConversions);
store.register_late_pass(|| box integer_division::IntegerDivision);
store.register_late_pass(|| box inherent_to_string::InherentToString);
let max_trait_bounds = conf.max_trait_bounds;
Expand Down Expand Up @@ -1134,7 +1137,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_early_pass(|| box needless_continue::NeedlessContinue);
store.register_late_pass(|| box create_dir::CreateDir);
store.register_early_pass(|| box needless_arbitrary_self_type::NeedlessArbitrarySelfType);
store.register_early_pass(|| box redundant_static_lifetimes::RedundantStaticLifetimes);
store.register_late_pass(|| box cargo_common_metadata::CargoCommonMetadata);
store.register_late_pass(|| box multiple_crate_versions::MultipleCrateVersions);
store.register_late_pass(|| box wildcard_dependencies::WildcardDependencies);
Expand Down Expand Up @@ -1173,7 +1175,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box mut_mutex_lock::MutMutexLock);
store.register_late_pass(|| box match_on_vec_items::MatchOnVecItems);
store.register_late_pass(|| box manual_async_fn::ManualAsyncFn);
store.register_early_pass(|| box redundant_field_names::RedundantFieldNames);
store.register_late_pass(|| box vec_resize_to_zero::VecResizeToZero);
store.register_late_pass(|| box panic_in_result_fn::PanicInResultFn);
let single_char_binding_names_threshold = conf.single_char_binding_names_threshold;
Expand Down
11 changes: 9 additions & 2 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
["next", "iter"] => lint_iter_next(cx, expr, arg_lists[1]),
["map", "filter"] => lint_filter_map(cx, expr, arg_lists[1], arg_lists[0]),
["map", "filter_map"] => lint_filter_map_map(cx, expr, arg_lists[1], arg_lists[0]),
["next", "filter_map"] => lint_filter_map_next(cx, expr, arg_lists[1]),
["next", "filter_map"] => lint_filter_map_next(cx, expr, arg_lists[1], self.msrv.as_ref()),
["map", "find"] => lint_find_map(cx, expr, arg_lists[1], arg_lists[0]),
["flat_map", "filter"] => lint_filter_flat_map(cx, expr, arg_lists[1], arg_lists[0]),
["flat_map", "filter_map"] => lint_filter_map_flat_map(cx, expr, arg_lists[1], arg_lists[0]),
Expand Down Expand Up @@ -2923,9 +2923,16 @@ fn lint_filter_map<'tcx>(
}
}

const FILTER_MAP_NEXT_MSRV: RustcVersion = RustcVersion::new(1, 30, 0);

/// lint use of `filter_map().next()` for `Iterators`
fn lint_filter_map_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, filter_args: &'tcx [hir::Expr<'_>]) {
fn lint_filter_map_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, filter_args: &'tcx [hir::Expr<'_>], msrv: Option<&RustcVersion>) {
if match_trait_method(cx, expr, &paths::ITERATOR) {

if !meets_msrv(msrv, &FILTER_MAP_NEXT_MSRV) {
return;
}

let msg = "called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling \
`.find_map(..)` instead.";
let filter_snippet = snippet(cx, filter_args[1].span, "..");
Expand Down
27 changes: 24 additions & 3 deletions clippy_lints/src/redundant_field_names.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::utils::span_lint_and_sugg;
use crate::utils::{meets_msrv, span_lint_and_sugg};
use rustc_ast::ast::{Expr, ExprKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_semver::RustcVersion;
use rustc_session::{impl_lint_pass, declare_tool_lint};

const REDUNDANT_FIELD_NAMES_MSRV: RustcVersion = RustcVersion::new(1, 17, 0);

declare_clippy_lint! {
/// **What it does:** Checks for fields in struct literals where shorthands
Expand Down Expand Up @@ -33,10 +36,26 @@ declare_clippy_lint! {
"checks for fields in struct literals where shorthands could be used"
}

declare_lint_pass!(RedundantFieldNames => [REDUNDANT_FIELD_NAMES]);
pub struct RedundantFieldNames {
msrv: Option<RustcVersion>,
}

impl RedundantFieldNames {
#[must_use]
pub fn new(msrv: Option<RustcVersion>) -> Self {
Self { msrv }
}
}

impl_lint_pass!(RedundantFieldNames => [REDUNDANT_FIELD_NAMES]);

impl EarlyLintPass for RedundantFieldNames {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {

if !meets_msrv(self.msrv.as_ref(), &REDUNDANT_FIELD_NAMES_MSRV) {
return;
}

if in_external_macro(cx.sess, expr.span) {
return;
}
Expand Down Expand Up @@ -64,4 +83,6 @@ impl EarlyLintPass for RedundantFieldNames {
}
}
}

extract_msrv_attr!(EarlyContext);
}
27 changes: 24 additions & 3 deletions clippy_lints/src/redundant_static_lifetimes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::utils::{snippet, span_lint_and_then};
use crate::utils::{meets_msrv, snippet, span_lint_and_then};
use rustc_ast::ast::{Item, ItemKind, Ty, TyKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_semver::RustcVersion;
use rustc_session::{impl_lint_pass, declare_tool_lint};

const REDUNDANT_STATIC_LIFETIMES_MSRV: RustcVersion = RustcVersion::new(1, 17, 0);

declare_clippy_lint! {
/// **What it does:** Checks for constants and statics with an explicit `'static` lifetime.
Expand All @@ -29,7 +32,18 @@ declare_clippy_lint! {
"Using explicit `'static` lifetime for constants or statics when elision rules would allow omitting them."
}

declare_lint_pass!(RedundantStaticLifetimes => [REDUNDANT_STATIC_LIFETIMES]);
pub struct RedundantStaticLifetimes {
msrv: Option<RustcVersion>,
}

impl RedundantStaticLifetimes {
#[must_use]
pub fn new(msrv: Option<RustcVersion>) -> Self {
Self { msrv }
}
}

impl_lint_pass!(RedundantStaticLifetimes => [REDUNDANT_STATIC_LIFETIMES]);

impl RedundantStaticLifetimes {
// Recursively visit types
Expand Down Expand Up @@ -84,6 +98,11 @@ impl RedundantStaticLifetimes {

impl EarlyLintPass for RedundantStaticLifetimes {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {

if !meets_msrv(self.msrv.as_ref(), &REDUNDANT_STATIC_LIFETIMES_MSRV) {
return;
}

if !item.span.from_expansion() {
if let ItemKind::Const(_, ref var_type, _) = item.kind {
self.visit_type(var_type, cx, "constants have by default a `'static` lifetime");
Expand All @@ -96,4 +115,6 @@ impl EarlyLintPass for RedundantStaticLifetimes {
}
}
}

extract_msrv_attr!(EarlyContext);
}
38 changes: 37 additions & 1 deletion tests/ui/min_rust_version_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![feature(custom_inner_attributes)]
#![clippy::msrv = "1.0.0"]

use std::ops::Deref;
use std::ops::{RangeFrom, Deref};

fn option_as_ref_deref() {
let mut opt = Some(String::from("123"));
Expand Down Expand Up @@ -42,7 +42,43 @@ pub fn manual_strip_msrv() {
}
}

pub fn redundant_fieldnames() {
let start = 0;
let _ = RangeFrom { start: start };
}

pub fn redundant_static_lifetime() {
const VAR_ONE: &'static str = "Test constant #1";
}

pub fn checked_conversion() {
let value: i64 = 42;
let _ = value <= (u32::max_value() as i64) && value >= 0;
let _ = value <= (u32::MAX as i64) && value >= 0;
}

pub fn filter_map_next() {
let a = ["1", "lol", "3", "NaN", "5"];

#[rustfmt::skip]
let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
.into_iter()
.filter_map(|x| {
if x == 2 {
Some(x * 2)
} else {
None
}
})
.next();
}


fn main() {
filter_map_next();
checked_conversion();
redundant_fieldnames();
redundant_static_lifetime();
option_as_ref_deref();
match_like_matches();
match_same_arms();
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/min_rust_version_attr.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
error: stripping a prefix manually
--> $DIR/min_rust_version_attr.rs:60:24
--> $DIR/min_rust_version_attr.rs:96:24
|
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
| ^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::manual-strip` implied by `-D warnings`
note: the prefix was tested here
--> $DIR/min_rust_version_attr.rs:59:9
--> $DIR/min_rust_version_attr.rs:95:9
|
LL | if s.starts_with("hello, ") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -17,13 +17,13 @@ LL | assert_eq!(<stripped>.to_uppercase(), "WORLD!");
|

error: stripping a prefix manually
--> $DIR/min_rust_version_attr.rs:72:24
--> $DIR/min_rust_version_attr.rs:108:24
|
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
| ^^^^^^^^^^^^^^^^^^^^
|
note: the prefix was tested here
--> $DIR/min_rust_version_attr.rs:71:9
--> $DIR/min_rust_version_attr.rs:107:9
|
LL | if s.starts_with("hello, ") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 66d3509

Please sign in to comment.