Skip to content

Rustup #2920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2018
Merged

Rustup #2920

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/enum_glob_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {

impl EnumGlobUse {
fn lint_item(&self, cx: &LateContext, item: &Item) {
if item.vis.node == VisibilityKind::Public {
if item.vis.node.is_pub() {
return; // re-exports are fine
}
if let ItemUse(ref path, UseKind::Glob) = item.node {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/enum_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl EarlyLintPass for EnumVariantNames {
);
}
}
if item.vis.node == VisibilityKind::Public {
if item.vis.node.is_pub() {
let matching = partial_match(mod_camel, &item_camel);
let rmatching = partial_rmatch(mod_camel, &item_camel);
let nchars = mod_camel.chars().count();
Expand Down
18 changes: 11 additions & 7 deletions clippy_lints/src/if_let_redundant_pattern_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if let ExprMatch(ref op, ref arms, MatchSource::IfLetDesugar { .. }) = expr.node {
if arms[0].pats.len() == 1 {
let good_method = match arms[0].pats[0].node {
PatKind::TupleStruct(ref path, ref pats, _) if pats.len() == 1 && pats[0].node == PatKind::Wild => {
if match_qpath(path, &paths::RESULT_OK) {
"is_ok()"
} else if match_qpath(path, &paths::RESULT_ERR) {
"is_err()"
} else if match_qpath(path, &paths::OPTION_SOME) {
"is_some()"
PatKind::TupleStruct(ref path, ref pats, _) if pats.len() == 1 => {
if let PatKind::Wild = pats[0].node {
if match_qpath(path, &paths::RESULT_OK) {
"is_ok()"
} else if match_qpath(path, &paths::RESULT_ERR) {
"is_err()"
} else if match_qpath(path, &paths::OPTION_SOME) {
"is_some()"
} else {
return;
}
} else {
return;
}
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,10 @@ fn has_is_empty(cx: &LateContext, expr: &Expr) -> bool {

let ty = &walk_ptrs_ty(cx.tables.expr_ty(expr));
match ty.sty {
ty::TyDynamic(..) => cx.tcx
.associated_items(ty.ty_to_def_id().expect("trait impl not found"))
ty::TyDynamic(ref tt, ..) => cx.tcx
.associated_items(tt.principal().expect("trait impl not found").def_id())
.any(|item| is_is_empty(cx, &item)),
ty::TyProjection(_) => ty.ty_to_def_id()
.map_or(false, |id| has_is_empty_impl(cx, id)),
ty::TyProjection(ref proj) => has_is_empty_impl(cx, proj.item_def_id),
ty::TyAdt(id, _) => has_is_empty_impl(cx, id.did),
ty::TyArray(..) | ty::TySlice(..) | ty::TyStr => true,
_ => false,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::consts::{constant, Constant};

use crate::utils::{get_enclosing_block, get_parent_expr, higher, in_external_macro, is_integer_literal, is_refutable,
last_path_segment, match_trait_method, match_type, match_var, multispan_sugg, snippet, snippet_opt,
span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then};
span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, SpanlessEq};
use crate::utils::paths;

/// **What it does:** Checks for for-loops that manually copy items between
Expand Down Expand Up @@ -1955,7 +1955,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
if self.state == VarState::DontWarn {
return;
}
if expr == self.end_expr {
if SpanlessEq::new(self.cx).eq_expr(&expr, self.end_expr) {
self.past_loop = true;
return;
}
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/map_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc::hir::*;
use rustc::ty;
use syntax::ast;
use crate::utils::{get_arg_ident, is_adjusted, iter_input_pats, match_qpath, match_trait_method, match_type,
paths, remove_blocks, snippet, span_help_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth};
paths, remove_blocks, snippet, span_help_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq};

/// **What it does:** Checks for mapping `clone()` over an iterator.
///
Expand Down Expand Up @@ -66,7 +66,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if clone_call.ident.name == "clone" &&
clone_args.len() == 1 &&
match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) &&
expr_eq_name(&clone_args[0], arg_ident)
expr_eq_name(cx, &clone_args[0], arg_ident)
{
span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
"you seem to be using .map() to clone the contents of an {}, consider \
Expand Down Expand Up @@ -98,7 +98,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
}
}

fn expr_eq_name(expr: &Expr, id: ast::Ident) -> bool {
fn expr_eq_name(cx: &LateContext, expr: &Expr, id: ast::Ident) -> bool {
match expr.node {
ExprPath(QPath::Resolved(None, ref path)) => {
let arg_segment = [
Expand All @@ -108,7 +108,7 @@ fn expr_eq_name(expr: &Expr, id: ast::Ident) -> bool {
infer_types: true,
},
];
!path.is_global() && path.segments[..] == arg_segment
!path.is_global() && SpanlessEq::new(cx).eq_path_segments(&path.segments[..], &arg_segment)
},
_ => false,
}
Expand All @@ -127,7 +127,7 @@ fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static s
fn only_derefs(cx: &LateContext, expr: &Expr, id: ast::Ident) -> bool {
match expr.node {
ExprUnary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => only_derefs(cx, subexpr, id),
_ => expr_eq_name(expr, id),
_ => expr_eq_name(cx, expr, id),
}
}

Expand Down
13 changes: 10 additions & 3 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
}

fn check_single_match_single_pattern(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr, els: Option<&Expr>) {
if arms[1].pats[0].node == PatKind::Wild {
if is_wild(&arms[1].pats[0]) {
report_single_match_single_pattern(cx, ex, arms, expr, els);
}
}
Expand Down Expand Up @@ -265,7 +265,7 @@ fn check_single_match_opt_like(cx: &LateContext, ex: &Expr, arms: &[Arm], expr:
let path = match arms[1].pats[0].node {
PatKind::TupleStruct(ref path, ref inner, _) => {
// contains any non wildcard patterns? e.g. Err(err)
if inner.iter().any(|pat| pat.node != PatKind::Wild) {
if !inner.iter().all(is_wild) {
return;
}
print::to_string(print::NO_ANN, |s| s.print_qpath(path, false))
Expand Down Expand Up @@ -356,6 +356,13 @@ fn check_overlapping_arms<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ex: &'tcx Expr,
}
}

fn is_wild(pat: &impl std::ops::Deref<Target = Pat>) -> bool {
match pat.node {
PatKind::Wild => true,
_ => false,
}
}

fn check_wild_err_arm(cx: &LateContext, ex: &Expr, arms: &[Arm]) {
let ex_ty = walk_ptrs_ty(cx.tables.expr_ty(ex));
if match_type(cx, ex_ty, &paths::RESULT) {
Expand All @@ -364,7 +371,7 @@ fn check_wild_err_arm(cx: &LateContext, ex: &Expr, arms: &[Arm]) {
let path_str = print::to_string(print::NO_ANN, |s| s.print_qpath(path, false));
if_chain! {
if path_str == "Err";
if inner.iter().any(|pat| pat.node == PatKind::Wild);
if inner.iter().any(is_wild);
if let ExprBlock(ref block, _) = arm.body.node;
if is_panic_block(block);
then {
Expand Down
20 changes: 11 additions & 9 deletions clippy_lints/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use syntax::codemap::{Span, BytePos};
use crate::utils::{get_arg_name, get_trait_def_id, implements_trait, in_external_macro, in_macro, is_copy, is_expn_of, is_self,
is_self_ty, iter_input_pats, last_path_segment, match_def_path, match_path, match_qpath, match_trait_method,
match_type, method_chain_args, match_var, return_ty, remove_blocks, same_tys, single_segment_path, snippet,
span_lint, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth};
span_lint, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq};
use crate::utils::paths;
use crate::utils::sugg;
use crate::consts::{constant, Constant};
Expand Down Expand Up @@ -820,8 +820,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
for &(method_name, n_args, self_kind, out_type, trait_name) in &TRAIT_METHODS {
if name == method_name &&
sig.decl.inputs.len() == n_args &&
out_type.matches(&sig.decl.output) &&
self_kind.matches(first_arg_ty, first_arg, self_ty, false, &implitem.generics) {
out_type.matches(cx, &sig.decl.output) &&
self_kind.matches(cx, first_arg_ty, first_arg, self_ty, false, &implitem.generics) {
span_lint(cx, SHOULD_IMPLEMENT_TRAIT, implitem.span, &format!(
"defining a method called `{}` on this type; consider implementing \
the `{}` trait or choosing a less ambiguous name", name, trait_name));
Expand All @@ -838,9 +838,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if conv.check(&name.as_str());
if !self_kinds
.iter()
.any(|k| k.matches(first_arg_ty, first_arg, self_ty, is_copy, &implitem.generics));
.any(|k| k.matches(cx, first_arg_ty, first_arg, self_ty, is_copy, &implitem.generics));
then {
let lint = if item.vis.node == hir::VisibilityKind::Public {
let lint = if item.vis.node.is_pub() {
WRONG_PUB_SELF_CONVENTION
} else {
WRONG_SELF_CONVENTION
Expand Down Expand Up @@ -2030,6 +2030,7 @@ enum SelfKind {
impl SelfKind {
fn matches(
self,
cx: &LateContext,
ty: &hir::Ty,
arg: &hir::Arg,
self_ty: &hir::Ty,
Expand All @@ -2047,7 +2048,7 @@ impl SelfKind {
// `Self`, `&mut Self`,
// and `Box<Self>`, including the equivalent types with `Foo`.

let is_actually_self = |ty| is_self_ty(ty) || ty == self_ty;
let is_actually_self = |ty| is_self_ty(ty) || SpanlessEq::new(cx).eq_ty(ty, self_ty);
if is_self(arg) {
match self {
SelfKind::Value => is_actually_self(ty),
Expand Down Expand Up @@ -2173,12 +2174,13 @@ enum OutType {
}

impl OutType {
fn matches(self, ty: &hir::FunctionRetTy) -> bool {
fn matches(self, cx: &LateContext, ty: &hir::FunctionRetTy) -> bool {
let is_unit = |ty: &hir::Ty| SpanlessEq::new(cx).eq_ty_kind(&ty.node, &hir::TyTup(vec![].into()));
match (self, ty) {
(OutType::Unit, &hir::DefaultReturn(_)) => true,
(OutType::Unit, &hir::Return(ref ty)) if ty.node == hir::TyTup(vec![].into()) => true,
(OutType::Unit, &hir::Return(ref ty)) if is_unit(ty) => true,
(OutType::Bool, &hir::Return(ref ty)) if is_bool(ty) => true,
(OutType::Any, &hir::Return(ref ty)) if ty.node != hir::TyTup(vec![].into()) => true,
(OutType::Any, &hir::Return(ref ty)) if !is_unit(ty) => true,
(OutType::Ref, &hir::Return(ref ty)) => matches!(ty.node, hir::TyRptr(_, _)),
_ => false,
}
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc::ty;
use syntax::codemap::{ExpnFormat, Span};
use crate::utils::{get_item_name, get_parent_expr, implements_trait, in_constant, in_macro, is_integer_literal,
iter_input_pats, last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint,
span_lint_and_then, walk_ptrs_ty};
span_lint_and_then, walk_ptrs_ty, SpanlessEq};
use crate::utils::sugg::Sugg;
use syntax::ast::{LitKind, CRATE_NODE_ID};
use crate::consts::{constant, Constant};
Expand Down Expand Up @@ -418,7 +418,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {

fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
if let PatKind::Binding(_, _, ident, Some(ref right)) = pat.node {
if right.node == PatKind::Wild {
if let PatKind::Wild = right.node {
span_lint(
cx,
REDUNDANT_PATTERN,
Expand Down Expand Up @@ -542,7 +542,7 @@ fn check_to_owned(cx: &LateContext, expr: &Expr, other: &Expr) {
fn is_used(cx: &LateContext, expr: &Expr) -> bool {
if let Some(parent) = get_parent_expr(cx, expr) {
match parent.node {
ExprAssign(_, ref rhs) | ExprAssignOp(_, _, ref rhs) => **rhs == *expr,
ExprAssign(_, ref rhs) | ExprAssignOp(_, _, ref rhs) => SpanlessEq::new(cx).eq_expr(rhs, expr),
_ => is_used(cx, parent),
}
} else {
Expand Down
11 changes: 6 additions & 5 deletions clippy_lints/src/misc_early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl EarlyLintPass for MiscEarly {
.name;

for field in pfields {
if field.node.pat.node == PatKind::Wild {
if let PatKind::Wild = field.node.pat.node {
wilds += 1;
}
}
Expand All @@ -231,14 +231,15 @@ impl EarlyLintPass for MiscEarly {
let mut normal = vec![];

for field in pfields {
if field.node.pat.node != PatKind::Wild {
if let Ok(n) = cx.sess().codemap().span_to_snippet(field.span) {
match field.node.pat.node {
PatKind::Wild => {},
_ => if let Ok(n) = cx.sess().codemap().span_to_snippet(field.span) {
normal.push(n);
}
},
}
}
for field in pfields {
if field.node.pat.node == PatKind::Wild {
if let PatKind::Wild = field.node.pat.node {
wilds -= 1;
if wilds > 0 {
span_lint(
Expand Down
7 changes: 4 additions & 3 deletions clippy_lints/src/overflow_check_conditional.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc::lint::*;
use rustc::hir::*;
use crate::utils::span_lint;
use crate::utils::{span_lint, SpanlessEq};

/// **What it does:** Detects classic underflow/overflow checks.
///
Expand Down Expand Up @@ -31,13 +31,14 @@ impl LintPass for OverflowCheckConditional {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
// a + b < a, a > a + b, a < a - b, a - b > a
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
if_chain! {
if let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node;
if let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = first.node;
if let Expr_::ExprPath(QPath::Resolved(_, ref path1)) = ident1.node;
if let Expr_::ExprPath(QPath::Resolved(_, ref path2)) = ident2.node;
if let Expr_::ExprPath(QPath::Resolved(_, ref path3)) = second.node;
if path1.segments[0] == path3.segments[0] || path2.segments[0] == path3.segments[0];
if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
if cx.tables.expr_ty(ident1).is_integral();
if cx.tables.expr_ty(ident2).is_integral();
then {
Expand All @@ -62,7 +63,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
if let Expr_::ExprPath(QPath::Resolved(_, ref path1)) = ident1.node;
if let Expr_::ExprPath(QPath::Resolved(_, ref path2)) = ident2.node;
if let Expr_::ExprPath(QPath::Resolved(_, ref path3)) = first.node;
if path1.segments[0] == path3.segments[0] || path2.segments[0] == path3.segments[0];
if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
if cx.tables.expr_ty(ident1).is_integral();
if cx.tables.expr_ty(ident2).is_integral();
then {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc::hir::*;
use syntax::ast::RangeLimits;
use syntax::codemap::Spanned;
use crate::utils::{is_integer_literal, paths, snippet, span_lint, span_lint_and_then};
use crate::utils::{get_trait_def_id, higher, implements_trait};
use crate::utils::{get_trait_def_id, higher, implements_trait, SpanlessEq};
use crate::utils::sugg::Sugg;

/// **What it does:** Checks for calling `.step_by(0)` on iterators,
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
// .iter() and .len() called on same Path
if let ExprPath(QPath::Resolved(_, ref iter_path)) = iter_args[0].node;
if let ExprPath(QPath::Resolved(_, ref len_path)) = len_args[0].node;
if iter_path.segments == len_path.segments;
if SpanlessEq::new(cx).eq_path_segments(&iter_path.segments, &len_path.segments);
then {
span_lint(cx,
RANGE_ZIP_WITH_LEN,
Expand Down
Loading