Skip to content

Commit

Permalink
Rename lint to index_refutable_slice and connected config
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Sep 13, 2021
1 parent 3824dae commit 1ed88fa
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2559,7 +2559,6 @@ Released 2018-09-13
[`assign_op_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_op_pattern
[`assign_ops`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_ops
[`async_yields_async`]: https://rust-lang.github.io/rust-clippy/master/index.html#async_yields_async
[`avoidable_slice_indexing`]: https://rust-lang.github.io/rust-clippy/master/index.html#avoidable_slice_indexing
[`await_holding_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_lock
[`await_holding_refcell_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_refcell_ref
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask
Expand Down Expand Up @@ -2698,6 +2697,7 @@ Released 2018-09-13
[`imprecise_flops`]: https://rust-lang.github.io/rust-clippy/master/index.html#imprecise_flops
[`inconsistent_digit_grouping`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_digit_grouping
[`inconsistent_struct_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_struct_constructor
[`index_refutable_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#index_refutable_slice
[`indexing_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing
[`ineffective_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#ineffective_bit_mask
[`inefficient_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#inefficient_to_string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,34 @@ declare_clippy_lint! {
/// println!("{}", first);
/// }
/// ```
pub AVOIDABLE_SLICE_INDEXING,
pub INDEX_REFUTABLE_SLICE,
nursery,
"avoid indexing on slices which could be deconstructed"
}

#[derive(Copy, Clone)]
pub struct AvoidableSliceIndexing {
pub struct IndexRefutableSlice {
indexing_limit: u64,
msrv: Option<RustcVersion>,
}

impl AvoidableSliceIndexing {
pub fn new(avoidable_slice_indexing_limit: u64, msrv: Option<RustcVersion>) -> Self {
impl IndexRefutableSlice {
pub fn new(max_suggested_slice_pattern_length: u64, msrv: Option<RustcVersion>) -> Self {
Self {
indexing_limit: avoidable_slice_indexing_limit,
indexing_limit: max_suggested_slice_pattern_length,
msrv,
}
}
}

impl_lint_pass!(AvoidableSliceIndexing => [AVOIDABLE_SLICE_INDEXING]);
impl_lint_pass!(IndexRefutableSlice => [INDEX_REFUTABLE_SLICE]);

impl LateLintPass<'_> for AvoidableSliceIndexing {
impl LateLintPass<'_> for IndexRefutableSlice {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
if_chain! {
if !in_macro(expr.span) || is_expn_of(expr.span, "if_chain").is_some();
if let Some(IfLet {let_pat, if_then, ..}) = IfLet::hir(cx, expr);
if !is_lint_allowed(cx, AVOIDABLE_SLICE_INDEXING, expr.hir_id);
if !is_lint_allowed(cx, INDEX_REFUTABLE_SLICE, expr.hir_id);
if meets_msrv(self.msrv.as_ref(), &msrvs::SLICE_PATTERNS);
if let Some(found_slices) = find_slice_values(cx, let_pat);
if let Some(filtered_slices) = filter_lintable_slices(cx, found_slices, self.indexing_limit, if_then);
Expand Down Expand Up @@ -154,7 +154,7 @@ fn lint_slice(cx: &LateContext<'_>, slice: &SliceLintInformation) {

span_lint_and_then(
cx,
AVOIDABLE_SLICE_INDEXING,
INDEX_REFUTABLE_SLICE,
slice.ident.span,
"this binding can be a slice pattern to avoid indexing",
|diag| {
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ mod assertions_on_constants;
mod assign_ops;
mod async_yields_async;
mod attrs;
mod avoidable_slice_indexing;
mod await_holding_invalid;
mod bit_mask;
mod blacklisted_name;
Expand Down Expand Up @@ -233,6 +232,7 @@ mod implicit_hasher;
mod implicit_return;
mod implicit_saturating_sub;
mod inconsistent_struct_constructor;
mod index_refutable_slice;
mod indexing_slicing;
mod infinite_iter;
mod inherent_impl;
Expand Down Expand Up @@ -547,7 +547,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
attrs::INLINE_ALWAYS,
attrs::MISMATCHED_TARGET_OS,
attrs::USELESS_ATTRIBUTE,
avoidable_slice_indexing::AVOIDABLE_SLICE_INDEXING,
await_holding_invalid::AWAIT_HOLDING_LOCK,
await_holding_invalid::AWAIT_HOLDING_REFCELL_REF,
bit_mask::BAD_BIT_MASK,
Expand Down Expand Up @@ -664,6 +663,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
implicit_return::IMPLICIT_RETURN,
implicit_saturating_sub::IMPLICIT_SATURATING_SUB,
inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR,
index_refutable_slice::INDEX_REFUTABLE_SLICE,
indexing_slicing::INDEXING_SLICING,
indexing_slicing::OUT_OF_BOUNDS_INDEXING,
infinite_iter::INFINITE_ITER,
Expand Down Expand Up @@ -1805,7 +1805,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:

store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
LintId::of(attrs::EMPTY_LINE_AFTER_OUTER_ATTR),
LintId::of(avoidable_slice_indexing::AVOIDABLE_SLICE_INDEXING),
LintId::of(cognitive_complexity::COGNITIVE_COMPLEXITY),
LintId::of(copies::BRANCHES_SHARING_CODE),
LintId::of(disallowed_method::DISALLOWED_METHOD),
Expand All @@ -1814,6 +1813,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(floating_point_arithmetic::IMPRECISE_FLOPS),
LintId::of(floating_point_arithmetic::SUBOPTIMAL_FLOPS),
LintId::of(future_not_send::FUTURE_NOT_SEND),
LintId::of(index_refutable_slice::INDEX_REFUTABLE_SLICE),
LintId::of(let_if_seq::USELESS_LET_IF_SEQ),
LintId::of(missing_const_for_fn::MISSING_CONST_FOR_FN),
LintId::of(mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL),
Expand Down Expand Up @@ -1923,8 +1923,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_early_pass(move || Box::new(unnested_or_patterns::UnnestedOrPatterns::new(msrv)));

store.register_late_pass(|| Box::new(size_of_in_element_count::SizeOfInElementCount));
let avoidable_slice_indexing_limit = conf.avoidable_slice_indexing_limit;
store.register_late_pass(move || Box::new(avoidable_slice_indexing::AvoidableSliceIndexing::new(avoidable_slice_indexing_limit, msrv)));
let max_suggested_slice_pattern_length = conf.max_suggested_slice_pattern_length;
store.register_late_pass(move || Box::new(index_refutable_slice::IndexRefutableSlice::new(max_suggested_slice_pattern_length, msrv)));
store.register_late_pass(|| Box::new(map_clone::MapClone));
store.register_late_pass(|| Box::new(map_err_ignore::MapErrIgnore));
store.register_late_pass(|| Box::new(shadow::Shadow));
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/utils/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ define_Conf! {
///
/// Suppress lints whenever the suggested change would cause breakage for other crates.
(avoid_breaking_exported_api: bool = true),
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT, AVOIDABLE_SLICE_INDEXING.
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT, INDEX_REFUTABLE_SLICE.
///
/// The minimum rust version that the project supports
(msrv: Option<String> = None),
Expand Down Expand Up @@ -276,12 +276,12 @@ define_Conf! {
///
/// The list of unicode scripts allowed to be used in the scope.
(allowed_scripts: Vec<String> = vec!["Latin".to_string()]),
/// Lint: AVOIDABLE_SLICE_INDEXING.
/// Lint: INDEX_REFUTABLE_SLICE.
///
/// The limit after which slice indexing will not be linted as avoidable.
///
/// As an example, the default limit of `3` would allow `slice[3]` but lint `slice[2]`.
(avoidable_slice_indexing_limit: u64 = 3),
(max_suggested_slice_pattern_length: u64 = 3),
}

/// Search for the configuration file.
Expand Down
1 change: 0 additions & 1 deletion tests/ui-toml/avoidable_slice_indexing_limit/clippy.toml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(clippy::avoidable_slice_indexing)]
#![deny(clippy::index_refutable_slice)]

fn below_limit() {
let slice: Option<&[u32]> = Some(&[1, 2, 3]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ LL | if let Some(slice) = slice {
note: the lint level is defined here
--> $DIR/avoidable_slice_indexing_limit.rs:1:9
|
LL | #![deny(clippy::avoidable_slice_indexing)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | #![deny(clippy::index_refutable_slice)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try using a slice pattern here
|
LL | if let Some([_, _, _, _, _, _, _, slice_7, ..]) = slice {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max-suggested-slice-pattern-length = 8
2 changes: 1 addition & 1 deletion tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `avoidable-slice-indexing-limit`, `third-party` at line 5 column 1
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `max-suggested-slice-pattern-length`, `third-party` at line 5 column 1

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(clippy::avoidable_slice_indexing)]
#![deny(clippy::index_refutable_slice)]

enum SomeEnum<T> {
One(T),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error: this binding can be a slice pattern to avoid indexing
--> $DIR/if_let_slice_destruction.rs:13:17
--> $DIR/if_let_slice_binding.rs:13:17
|
LL | if let Some(slice) = slice {
| ^^^^^
|
note: the lint level is defined here
--> $DIR/if_let_slice_destruction.rs:1:9
--> $DIR/if_let_slice_binding.rs:1:9
|
LL | #![deny(clippy::avoidable_slice_indexing)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | #![deny(clippy::index_refutable_slice)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try using a slice pattern here
|
LL | if let Some([slice_0, ..]) = slice {
Expand All @@ -19,7 +19,7 @@ LL | println!("{}", slice_0);
| ~~~~~~~

error: this binding can be a slice pattern to avoid indexing
--> $DIR/if_let_slice_destruction.rs:19:17
--> $DIR/if_let_slice_binding.rs:19:17
|
LL | if let Some(slice) = slice {
| ^^^^^
Expand All @@ -34,7 +34,7 @@ LL | println!("{}", slice_0);
| ~~~~~~~

error: this binding can be a slice pattern to avoid indexing
--> $DIR/if_let_slice_destruction.rs:25:17
--> $DIR/if_let_slice_binding.rs:25:17
|
LL | if let Some(slice) = slice {
| ^^^^^
Expand All @@ -50,7 +50,7 @@ LL ~ println!("{}", slice_0);
|

error: this binding can be a slice pattern to avoid indexing
--> $DIR/if_let_slice_destruction.rs:32:26
--> $DIR/if_let_slice_binding.rs:32:26
|
LL | if let SomeEnum::One(slice) | SomeEnum::Three(slice) = slice_wrapped {
| ^^^^^
Expand All @@ -65,7 +65,7 @@ LL | println!("{}", slice_0);
| ~~~~~~~

error: this binding can be a slice pattern to avoid indexing
--> $DIR/if_let_slice_destruction.rs:39:29
--> $DIR/if_let_slice_binding.rs:39:29
|
LL | if let (SomeEnum::Three(a), Some(b)) = (a_wrapped, b_wrapped) {
| ^
Expand All @@ -80,7 +80,7 @@ LL | println!("{} -> {}", a_2, b[1]);
| ~~~

error: this binding can be a slice pattern to avoid indexing
--> $DIR/if_let_slice_destruction.rs:39:38
--> $DIR/if_let_slice_binding.rs:39:38
|
LL | if let (SomeEnum::Three(a), Some(b)) = (a_wrapped, b_wrapped) {
| ^
Expand All @@ -95,7 +95,7 @@ LL | println!("{} -> {}", a[2], b_1);
| ~~~

error: this binding can be a slice pattern to avoid indexing
--> $DIR/if_let_slice_destruction.rs:46:21
--> $DIR/if_let_slice_binding.rs:46:21
|
LL | if let Some(ref slice) = slice {
| ^^^^^
Expand All @@ -110,7 +110,7 @@ LL | println!("{:?}", slice_1);
| ~~~~~~~

error: this binding can be a slice pattern to avoid indexing
--> $DIR/if_let_slice_destruction.rs:54:17
--> $DIR/if_let_slice_binding.rs:54:17
|
LL | if let Some(slice) = &slice {
| ^^^^^
Expand All @@ -125,7 +125,7 @@ LL | println!("{:?}", slice_0);
| ~~~~~~~

error: this binding can be a slice pattern to avoid indexing
--> $DIR/if_let_slice_destruction.rs:123:17
--> $DIR/if_let_slice_binding.rs:123:17
|
LL | if let Some(slice) = wrap.inner {
| ^^^^^
Expand All @@ -140,7 +140,7 @@ LL | println!("This is awesome! {}", slice_0);
| ~~~~~~~

error: this binding can be a slice pattern to avoid indexing
--> $DIR/if_let_slice_destruction.rs:130:17
--> $DIR/if_let_slice_binding.rs:130:17
|
LL | if let Some(slice) = wrap.inner {
| ^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(clippy::avoidable_slice_indexing)]
#![deny(clippy::index_refutable_slice)]

extern crate if_chain;
use if_chain::if_chain;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ LL | if let Some(slice) = slice;
note: the lint level is defined here
--> $DIR/slice_indexing_in_macro.rs:1:9
|
LL | #![deny(clippy::avoidable_slice_indexing)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | #![deny(clippy::index_refutable_slice)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try using a slice pattern here
|
LL | if let Some([slice_0, ..]) = slice;
Expand Down

0 comments on commit 1ed88fa

Please sign in to comment.