Skip to content

Commit

Permalink
submodules: update clippy from 69f99e7 to cfb3320
Browse files Browse the repository at this point in the history
Changes:
````
rustup "Merge `ast::Mutability` and `mir::Mutability`"
rustup rust-lang/rust#67130
rustup rust-lang/rust#67455
There are no per-file copyright headers anymore
Update lints for `iterator_step_by_zero` changes
Fix 'redudant' spelling in redundant_clone docs
Fix documentation example for unnecessary_filter_map.
Fix `expect_fun_call` false negative on references
Fix `iterator_step_by_zero` description in declaration
Fix `iterator_step_by_zero` definition
Correct `iterator_step_by_zero` documentation
Update iterator_step_by_zero
Prevent `cmp_nan` when inside constants
Detect comparisons with NAN constants
Fix clippy build failure
````
  • Loading branch information
matthiaskrgr committed Dec 21, 2019
1 parent df5a1ee commit ee1be93
Show file tree
Hide file tree
Showing 34 changed files with 333 additions and 193 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,5 @@ Copyright 2014-2019 The Rust Project Developers
Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
[https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0)> or the MIT license
<LICENSE-MIT or [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT)>, at your
option. All files in the project carrying such notice may not be
option. Files in the project may not be
copied, modified, or distributed except according to those terms.
4 changes: 2 additions & 2 deletions clippy_lints/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ fn is_mutable_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>, span: Span,
Tuple(ref substs) => substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys)),
Array(ty, _) | Slice(ty) => is_mutable_ty(cx, ty, span, tys),
RawPtr(ty::TypeAndMut { ty, mutbl }) | Ref(_, ty, mutbl) => {
mutbl == hir::Mutability::Mutable || is_mutable_ty(cx, ty, span, tys)
mutbl == hir::Mutability::Mut || is_mutable_ty(cx, ty, span, tys)
},
// calling something constitutes a side effect, so return true on all callables
// also never calls need not be used, so return true for them, too
Expand Down Expand Up @@ -658,7 +658,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for StaticMutVisitor<'a, 'tcx> {
tys.clear();
}
},
Assign(ref target, _) | AssignOp(_, ref target, _) | AddrOf(_, hir::Mutability::Mutable, ref target) => {
Assign(ref target, _) | AssignOp(_, ref target, _) | AddrOf(_, hir::Mutability::Mut, ref target) => {
self.mutates_static |= is_mutated_static(self.cx, target)
},
_ => {},
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
#![feature(crate_visibility_modifier)]
#![feature(concat_idents)]
#![feature(result_map_or)]

// FIXME: switch to something more ergonomic here, once available.
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
Expand Down Expand Up @@ -607,6 +606,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
&methods::GET_UNWRAP,
&methods::INEFFICIENT_TO_STRING,
&methods::INTO_ITER_ON_REF,
&methods::ITERATOR_STEP_BY_ZERO,
&methods::ITER_CLONED_COLLECT,
&methods::ITER_NTH,
&methods::ITER_SKIP_NEXT,
Expand Down Expand Up @@ -700,7 +700,6 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
&ptr::PTR_ARG,
&ptr_offset_with_cast::PTR_OFFSET_WITH_CAST,
&question_mark::QUESTION_MARK,
&ranges::ITERATOR_STEP_BY_ZERO,
&ranges::RANGE_MINUS_ONE,
&ranges::RANGE_PLUS_ONE,
&ranges::RANGE_ZIP_WITH_LEN,
Expand Down Expand Up @@ -1180,6 +1179,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
LintId::of(&methods::FLAT_MAP_IDENTITY),
LintId::of(&methods::INEFFICIENT_TO_STRING),
LintId::of(&methods::INTO_ITER_ON_REF),
LintId::of(&methods::ITERATOR_STEP_BY_ZERO),
LintId::of(&methods::ITER_CLONED_COLLECT),
LintId::of(&methods::ITER_NTH),
LintId::of(&methods::ITER_SKIP_NEXT),
Expand Down Expand Up @@ -1245,7 +1245,6 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
LintId::of(&ptr::PTR_ARG),
LintId::of(&ptr_offset_with_cast::PTR_OFFSET_WITH_CAST),
LintId::of(&question_mark::QUESTION_MARK),
LintId::of(&ranges::ITERATOR_STEP_BY_ZERO),
LintId::of(&ranges::RANGE_MINUS_ONE),
LintId::of(&ranges::RANGE_PLUS_ONE),
LintId::of(&ranges::RANGE_ZIP_WITH_LEN),
Expand Down Expand Up @@ -1522,6 +1521,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
LintId::of(&mem_discriminant::MEM_DISCRIMINANT_NON_ENUM),
LintId::of(&mem_replace::MEM_REPLACE_WITH_UNINIT),
LintId::of(&methods::CLONE_DOUBLE_REF),
LintId::of(&methods::ITERATOR_STEP_BY_ZERO),
LintId::of(&methods::TEMPORARY_CSTRING_AS_PTR),
LintId::of(&methods::UNINIT_ASSUMED_INIT),
LintId::of(&methods::ZST_OFFSET),
Expand All @@ -1534,7 +1534,6 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
LintId::of(&non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST),
LintId::of(&open_options::NONSENSICAL_OPEN_OPTIONS),
LintId::of(&ptr::MUT_FROM_REF),
LintId::of(&ranges::ITERATOR_STEP_BY_ZERO),
LintId::of(&regex::INVALID_REGEX),
LintId::of(&serde_api::SERDE_API_MISUSE),
LintId::of(&suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL),
Expand Down
20 changes: 10 additions & 10 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,8 +1507,8 @@ fn make_iterator_snippet(cx: &LateContext<'_, '_>, arg: &Expr, applic_ref: &mut
if has_iter_method(cx, cx.tables.expr_ty(&arg_inner)).is_some() =>
{
let meth_name = match mutability {
Mutability::Mutable => "iter_mut",
Mutability::Immutable => "iter",
Mutability::Mut => "iter_mut",
Mutability::Not => "iter",
};
format!(
"{}.{}()",
Expand Down Expand Up @@ -1540,14 +1540,14 @@ fn check_for_loop_over_map_kv<'a, 'tcx>(
let (new_pat_span, kind, ty, mutbl) = match cx.tables.expr_ty(arg).kind {
ty::Ref(_, ty, mutbl) => match (&pat[0].kind, &pat[1].kind) {
(key, _) if pat_is_wild(key, body) => (pat[1].span, "value", ty, mutbl),
(_, value) if pat_is_wild(value, body) => (pat[0].span, "key", ty, Mutability::Immutable),
(_, value) if pat_is_wild(value, body) => (pat[0].span, "key", ty, Mutability::Not),
_ => return,
},
_ => return,
};
let mutbl = match mutbl {
Mutability::Immutable => "",
Mutability::Mutable => "_mut",
Mutability::Not => "",
Mutability::Mut => "_mut",
};
let arg = match arg.kind {
ExprKind::AddrOf(BorrowKind::Ref, _, ref expr) => &**expr,
Expand Down Expand Up @@ -1868,7 +1868,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
self.visit_expr(rhs);
},
ExprKind::AddrOf(BorrowKind::Ref, mutbl, ref expr) => {
if mutbl == Mutability::Mutable {
if mutbl == Mutability::Mut {
self.prefer_mutable = true;
}
self.visit_expr(expr);
Expand All @@ -1879,7 +1879,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
let ty = self.cx.tables.expr_ty_adjusted(expr);
self.prefer_mutable = false;
if let ty::Ref(_, _, mutbl) = ty.kind {
if mutbl == Mutability::Mutable {
if mutbl == Mutability::Mut {
self.prefer_mutable = true;
}
}
Expand All @@ -1891,7 +1891,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
for (ty, expr) in self.cx.tcx.fn_sig(def_id).inputs().skip_binder().iter().zip(args) {
self.prefer_mutable = false;
if let ty::Ref(_, _, mutbl) = ty.kind {
if mutbl == Mutability::Mutable {
if mutbl == Mutability::Mut {
self.prefer_mutable = true;
}
}
Expand Down Expand Up @@ -2084,7 +2084,7 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
}
},
ExprKind::Assign(ref lhs, _) if lhs.hir_id == expr.hir_id => *state = VarState::DontWarn,
ExprKind::AddrOf(BorrowKind::Ref, mutability, _) if mutability == Mutability::Mutable => {
ExprKind::AddrOf(BorrowKind::Ref, mutability, _) if mutability == Mutability::Mut => {
*state = VarState::DontWarn
},
_ => (),
Expand Down Expand Up @@ -2168,7 +2168,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
VarState::DontWarn
}
},
ExprKind::AddrOf(BorrowKind::Ref, mutability, _) if mutability == Mutability::Mutable => {
ExprKind::AddrOf(BorrowKind::Ref, mutability, _) if mutability == Mutability::Mut => {
self.state = VarState::DontWarn
},
_ => (),
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ fn is_panic_block(block: &Block) -> bool {
fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: &Expr) {
if has_only_ref_pats(arms) {
let mut suggs = Vec::new();
let (title, msg) = if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Immutable, ref inner) = ex.kind {
let (title, msg) = if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, ref inner) = ex.kind {
let span = ex.span.source_callsite();
suggs.push((span, Sugg::hir_with_macro_callsite(cx, inner, "..").to_string()));
(
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/mem_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemReplace {
// argument's type. All that's left is to get
// replacee's path.
let replaced_path = match func_args[0].kind {
ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mutable, ref replaced) => {
ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, ref replaced) => {
if let ExprKind::Path(QPath::Resolved(None, ref replaced_path)) = replaced.kind {
replaced_path
} else {
Expand Down
55 changes: 44 additions & 11 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,25 @@ declare_clippy_lint! {
"getting the inner pointer of a temporary `CString`"
}

declare_clippy_lint! {
/// **What it does:** Checks for calling `.step_by(0)` on iterators which panics.
///
/// **Why is this bad?** This very much looks like an oversight. Use `panic!()` instead if you
/// actually intend to panic.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```should_panic
/// for x in (0..100).step_by(0) {
/// //..
/// }
/// ```
pub ITERATOR_STEP_BY_ZERO,
correctness,
"using `Iterator::step_by(0)`, which will panic at runtime"
}

declare_clippy_lint! {
/// **What it does:** Checks for use of `.iter().nth()` (and the related
/// `.iter_mut().nth()`) on standard library types with O(1) element access.
Expand Down Expand Up @@ -958,11 +977,11 @@ declare_clippy_lint! {
/// ```
///
/// ```rust
/// let _ = (0..4).filter_map(i32::checked_abs);
/// let _ = (0..4).filter_map(|x| Some(x + 1));
/// ```
/// As there is no conditional check on the argument this could be written as:
/// ```rust
/// let _ = (0..4).map(i32::checked_abs);
/// let _ = (0..4).map(|x| x + 1);
/// ```
pub UNNECESSARY_FILTER_MAP,
complexity,
Expand Down Expand Up @@ -1115,6 +1134,7 @@ declare_lint_pass!(Methods => [
FLAT_MAP_IDENTITY,
FIND_MAP,
MAP_FLATTEN,
ITERATOR_STEP_BY_ZERO,
ITER_NTH,
ITER_SKIP_NEXT,
GET_UNWRAP,
Expand Down Expand Up @@ -1173,6 +1193,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
},
["nth", "iter"] => lint_iter_nth(cx, expr, arg_lists[1], false),
["nth", "iter_mut"] => lint_iter_nth(cx, expr, arg_lists[1], true),
["step_by", ..] => lint_step_by(cx, expr, arg_lists[0]),
["next", "skip"] => lint_iter_skip_next(cx, expr),
["collect", "cloned"] => lint_iter_cloned_collect(cx, expr, arg_lists[1]),
["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]),
Expand Down Expand Up @@ -1595,7 +1616,7 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span:
return;
}

let receiver_type = cx.tables.expr_ty(&args[0]);
let receiver_type = cx.tables.expr_ty_adjusted(&args[0]);
let closure_args = if match_type(cx, receiver_type, &paths::OPTION) {
"||"
} else if match_type(cx, receiver_type, &paths::RESULT) {
Expand Down Expand Up @@ -1950,6 +1971,20 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
}
}

fn lint_step_by<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr, args: &'tcx [hir::Expr]) {
if match_trait_method(cx, expr, &paths::ITERATOR) {
use crate::consts::{constant, Constant};
if let Some((Constant::Int(0), _)) = constant(cx, cx.tables, &args[1]) {
span_lint(
cx,
ITERATOR_STEP_BY_ZERO,
expr.span,
"Iterator::step_by(0) will panic at runtime",
);
}
}
}

fn lint_iter_nth<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr, iter_args: &'tcx [hir::Expr], is_mut: bool) {
let mut_str = if is_mut { "_mut" } else { "" };
let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0])).is_some() {
Expand Down Expand Up @@ -2789,8 +2824,8 @@ fn ty_has_iter_method(cx: &LateContext<'_, '_>, self_ref_ty: Ty<'_>) -> Option<(
_ => unreachable!(),
};
let method_name = match mutbl {
hir::Mutability::Immutable => "iter",
hir::Mutability::Mutable => "iter_mut",
hir::Mutability::Not => "iter",
hir::Mutability::Mut => "iter_mut",
};
(ty_name, method_name)
})
Expand Down Expand Up @@ -2980,8 +3015,8 @@ impl SelfKind {
}

let trait_path = match mutability {
hir::Mutability::Immutable => &paths::ASREF_TRAIT,
hir::Mutability::Mutable => &paths::ASMUT_TRAIT,
hir::Mutability::Not => &paths::ASREF_TRAIT,
hir::Mutability::Mut => &paths::ASMUT_TRAIT,
};

let trait_def_id = match get_trait_def_id(cx, trait_path) {
Expand All @@ -2993,10 +3028,8 @@ impl SelfKind {

match self {
Self::Value => matches_value(parent_ty, ty),
Self::Ref => {
matches_ref(cx, hir::Mutability::Immutable, parent_ty, ty) || ty == parent_ty && is_copy(cx, ty)
},
Self::RefMut => matches_ref(cx, hir::Mutability::Mutable, parent_ty, ty),
Self::Ref => matches_ref(cx, hir::Mutability::Not, parent_ty, ty) || ty == parent_ty && is_copy(cx, ty),
Self::RefMut => matches_ref(cx, hir::Mutability::Mut, parent_ty, ty),
Self::No => ty != parent_ty,
}
}
Expand Down
30 changes: 17 additions & 13 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
ExprKind::Binary(ref cmp, ref left, ref right) => {
let op = cmp.node;
if op.is_comparison() {
if let ExprKind::Path(QPath::Resolved(_, ref path)) = left.kind {
check_nan(cx, path, expr);
}
if let ExprKind::Path(QPath::Resolved(_, ref path)) = right.kind {
check_nan(cx, path, expr);
}
check_nan(cx, left, expr);
check_nan(cx, right, expr);
check_to_owned(cx, left, right);
check_to_owned(cx, right, left);
}
Expand Down Expand Up @@ -444,14 +440,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
}
}

fn check_nan(cx: &LateContext<'_, '_>, path: &Path, expr: &Expr) {
if !in_constant(cx, expr.hir_id) {
if let Some(seg) = path.segments.last() {
if seg.ident.name == sym!(NAN) {
fn check_nan(cx: &LateContext<'_, '_>, expr: &Expr, cmp_expr: &Expr) {
if_chain! {
if !in_constant(cx, cmp_expr.hir_id);
if let Some((value, _)) = constant(cx, cx.tables, expr);
then {
let needs_lint = match value {
Constant::F32(num) => num.is_nan(),
Constant::F64(num) => num.is_nan(),
_ => false,
};

if needs_lint {
span_lint(
cx,
CMP_NAN,
expr.span,
cmp_expr.span,
"doomed comparison with NAN, use `std::{f32,f64}::is_nan()` instead",
);
}
Expand Down Expand Up @@ -628,8 +632,8 @@ fn check_cast(cx: &LateContext<'_, '_>, span: Span, e: &Expr, ty: &Ty) {
if !in_constant(cx, e.hir_id);
then {
let (msg, sugg_fn) = match mut_ty.mutbl {
Mutability::Mutable => ("`0 as *mut _` detected", "std::ptr::null_mut"),
Mutability::Immutable => ("`0 as *const _` detected", "std::ptr::null"),
Mutability::Mut => ("`0 as *mut _` detected", "std::ptr::null_mut"),
Mutability::Not => ("`0 as *const _` detected", "std::ptr::null"),
};

let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind {
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/mut_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
// Let's ignore the generated code.
intravisit::walk_expr(self, arg);
intravisit::walk_expr(self, body);
} else if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mutable, ref e) = expr.kind {
if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mutable, _) = e.kind {
} else if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, ref e) = expr.kind {
if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, _) = e.kind {
span_lint(
self.cx,
MUT_MUT,
expr.span,
"generally you want to avoid `&mut &mut _` if possible",
);
} else if let ty::Ref(_, _, hir::Mutability::Mutable) = self.cx.tables.expr_ty(e).kind {
} else if let ty::Ref(_, _, hir::Mutability::Mut) = self.cx.tables.expr_ty(e).kind {
span_lint(
self.cx,
MUT_MUT,
Expand All @@ -82,14 +82,14 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
_,
hir::MutTy {
ty: ref pty,
mutbl: hir::Mutability::Mutable,
mutbl: hir::Mutability::Mut,
},
) = ty.kind
{
if let hir::TyKind::Rptr(
_,
hir::MutTy {
mutbl: hir::Mutability::Mutable,
mutbl: hir::Mutability::Mut,
..
},
) = pty.kind
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/mut_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ fn check_arguments<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arguments: &[Expr], typ
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
for (argument, parameter) in arguments.iter().zip(parameters.iter()) {
match parameter.kind {
ty::Ref(_, _, Mutability::Immutable)
ty::Ref(_, _, Mutability::Not)
| ty::RawPtr(ty::TypeAndMut {
mutbl: Mutability::Immutable,
..
mutbl: Mutability::Not, ..
}) => {
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mutable, _) = argument.kind {
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind {
span_lint(
cx,
UNNECESSARY_MUT_PASSED,
Expand Down
Loading

0 comments on commit ee1be93

Please sign in to comment.