Skip to content
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

Some rustups #5040

Merged
merged 8 commits into from
Jan 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
57 changes: 43 additions & 14 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::consts::{constant, Constant};
use crate::consts::{constant, miri_to_const, Constant};
use crate::utils::paths;
use crate::utils::sugg::Sugg;
use crate::utils::{
Expand Down Expand Up @@ -444,7 +444,7 @@ fn check_match_bool(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>], e

fn check_overlapping_arms<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ex: &'tcx Expr<'_>, arms: &'tcx [Arm<'_>]) {
if arms.len() >= 2 && cx.tables.expr_ty(ex).is_integral() {
let ranges = all_ranges(cx, arms);
let ranges = all_ranges(cx, arms, cx.tables.expr_ty(ex));
let type_ranges = type_ranges(&ranges);
if !type_ranges.is_empty() {
if let Some((start, end)) = overlapping(&type_ranges) {
Expand Down Expand Up @@ -705,24 +705,53 @@ fn check_wild_in_or_pats(cx: &LateContext<'_, '_>, arms: &[Arm<'_>]) {
}

/// Gets all arms that are unbounded `PatRange`s.
fn all_ranges<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arms: &'tcx [Arm<'_>]) -> Vec<SpannedRange<Constant>> {
fn all_ranges<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>,
arms: &'tcx [Arm<'_>],
ty: Ty<'tcx>,
) -> Vec<SpannedRange<Constant>> {
arms.iter()
.flat_map(|arm| {
if let Arm {
ref pat, guard: None, ..
} = *arm
{
if let PatKind::Range(ref lhs, ref rhs, ref range_end) = pat.kind {
let lhs = constant(cx, cx.tables, lhs)?.0;
let rhs = constant(cx, cx.tables, rhs)?.0;
let rhs = match *range_end {
RangeEnd::Included => Bound::Included(rhs),
RangeEnd::Excluded => Bound::Excluded(rhs),
};
return Some(SpannedRange {
span: pat.span,
node: (lhs, rhs),
});
if let PatKind::Range(ref lhs, ref rhs, range_end) = pat.kind {
match (lhs, rhs) {
(Some(lhs), Some(rhs)) => {
let lhs = constant(cx, cx.tables, lhs)?.0;
let rhs = constant(cx, cx.tables, rhs)?.0;
let rhs = match range_end {
RangeEnd::Included => Bound::Included(rhs),
RangeEnd::Excluded => Bound::Excluded(rhs),
};
return Some(SpannedRange {
span: pat.span,
node: (lhs, rhs),
});
},
(None, Some(rhs)) => {
let lhs = miri_to_const(ty.numeric_min_val(cx.tcx)?)?;
JohnTitor marked this conversation as resolved.
Show resolved Hide resolved
let rhs = constant(cx, cx.tables, rhs)?.0;
let rhs = match range_end {
RangeEnd::Included => Bound::Included(rhs),
RangeEnd::Excluded => Bound::Excluded(rhs),
};
return Some(SpannedRange {
span: pat.span,
node: (lhs, rhs),
});
},
(Some(lhs), None) => {
let lhs = constant(cx, cx.tables, lhs)?.0;
let rhs = miri_to_const(ty.numeric_max_val(cx.tcx)?)?;
return Some(SpannedRange {
span: pat.span,
node: (lhs, Bound::Excluded(rhs)),
});
},
_ => return None,
}
}

if let PatKind::Lit(ref value) = pat.kind {
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/needless_pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc::traits;
use rustc::traits::misc::can_type_implement_copy;
use rustc::ty::{self, RegionKind, TypeFoldable};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::Applicability;
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_hir::intravisit::FnKind;
use rustc_hir::*;
use rustc_session::declare_tool_lint;
Expand All @@ -20,7 +20,6 @@ use rustc_target::spec::abi::Abi;
use rustc_typeck::expr_use_visitor as euv;
use std::borrow::Cow;
use syntax::ast::Attribute;
use syntax::errors::DiagnosticBuilder;

declare_clippy_lint! {
/// **What it does:** Checks for functions taking arguments by value, but not
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintC
use rustc::ty::layout::LayoutOf;
use rustc::ty::{self, InferTy, Ty, TyCtxt, TypeckTables};
use rustc::{declare_lint_pass, impl_lint_pass};
use rustc_errors::Applicability;
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::intravisit::{walk_body, walk_expr, walk_ty, FnKind, NestedVisitorMap, Visitor};
use rustc_hir::*;
Expand All @@ -21,7 +21,6 @@ use rustc_span::symbol::{sym, Symbol};
use rustc_target::spec::abi::Abi;
use rustc_typeck::hir_ty_to_ty;
use syntax::ast::{FloatTy, IntTy, LitFloatType, LitIntType, LitKind, UintTy};
use syntax::errors::DiagnosticBuilder;

use crate::consts::{constant, Constant};
use crate::utils::paths;
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/utils/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, Pat, PatKind, QPath, Stmt, StmtKind, TyKind};
use rustc_session::declare_tool_lint;
use syntax::ast::{Attribute, LitFloatType, LitKind};
use syntax::walk_list;

declare_clippy_lint! {
/// **What it does:** Generates clippy code that detects the offending pattern
Expand Down Expand Up @@ -617,9 +618,9 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
start_pat, end_pat, end_kind, current
);
self.current = start_pat;
self.visit_expr(start);
walk_list!(self, visit_expr, start);
self.current = end_pat;
self.visit_expr(end);
walk_list!(self, visit_expr, end);
},
PatKind::Slice(ref start, ref middle, ref end) => {
let start_pat = self.next("start");
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/utils/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Clippy wrappers around rustc's diagnostic functions.

use rustc::lint::{LateContext, Lint, LintContext};
use rustc_errors::{Applicability, CodeSuggestion, Substitution, SubstitutionPart, SuggestionStyle};
use rustc_errors::{Applicability, CodeSuggestion, DiagnosticBuilder, Substitution, SubstitutionPart, SuggestionStyle};
use rustc_hir::HirId;
use rustc_span::source_map::{MultiSpan, Span};
use std::env;
use syntax::errors::DiagnosticBuilder;

/// Wrapper around `DiagnosticBuilder` that adds a link to Clippy documentation for the emitted lint
struct DiagnosticWrapper<'a>(DiagnosticBuilder<'a>);
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
(&PatKind::Tuple(ref l, ls), &PatKind::Tuple(ref r, rs)) => {
ls == rs && over(l, r, |l, r| self.eq_pat(l, r))
},
(&PatKind::Range(ref ls, ref le, ref li), &PatKind::Range(ref rs, ref re, ref ri)) => {
self.eq_expr(ls, rs) && self.eq_expr(le, re) && (*li == *ri)
(&PatKind::Range(ref ls, ref le, li), &PatKind::Range(ref rs, ref re, ri)) => {
both(ls, rs, |a, b| self.eq_expr(a, b)) && both(le, re, |a, b| self.eq_expr(a, b)) && (li == ri)
},
(&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
(&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
Expand Down
8 changes: 6 additions & 2 deletions clippy_lints/src/utils/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,12 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat<'_>, indent: usize) {
},
hir::PatKind::Range(ref l, ref r, ref range_end) => {
println!("{}Range", ind);
print_expr(cx, l, indent + 1);
print_expr(cx, r, indent + 1);
if let Some(expr) = l {
print_expr(cx, expr, indent + 1);
}
if let Some(expr) = r {
print_expr(cx, expr, indent + 1);
}
match *range_end {
hir::RangeEnd::Included => println!("{} end included", ind),
hir::RangeEnd::Excluded => println!("{} end excluded", ind),
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/match_overlapping_arm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(exclusive_range_pattern)]
#![feature(half_open_range_patterns)]
#![warn(clippy::match_overlapping_arm)]
#![allow(clippy::redundant_pattern_matching)]

Expand Down Expand Up @@ -56,6 +57,18 @@ fn overlapping() {
_ => (),
}

match 42 {
0.. => println!("0 .. 42"),
3.. => println!("3 .. 42"),
_ => (),
}

match 42 {
..=23 => println!("0 ... 23"),
..26 => println!("0 .. 26"),
_ => (),
}

if let None = Some(42) {
// nothing
} else if let None = Some(42) {
Expand Down
46 changes: 35 additions & 11 deletions tests/ui/match_overlapping_arm.stderr
Original file line number Diff line number Diff line change
@@ -1,63 +1,87 @@
error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:11:9
--> $DIR/match_overlapping_arm.rs:12:9
|
LL | 0..=10 => println!("0 ... 10"),
| ^^^^^^
|
= note: `-D clippy::match-overlapping-arm` implied by `-D warnings`
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:12:9
--> $DIR/match_overlapping_arm.rs:13:9
|
LL | 0..=11 => println!("0 ... 11"),
| ^^^^^^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:17:9
--> $DIR/match_overlapping_arm.rs:18:9
|
LL | 0..=5 => println!("0 ... 5"),
| ^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:19:9
--> $DIR/match_overlapping_arm.rs:20:9
|
LL | FOO..=11 => println!("0 ... 11"),
| ^^^^^^^^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:25:9
--> $DIR/match_overlapping_arm.rs:26:9
|
LL | 0..=5 => println!("0 ... 5"),
| ^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:24:9
--> $DIR/match_overlapping_arm.rs:25:9
|
LL | 2 => println!("2"),
| ^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:31:9
--> $DIR/match_overlapping_arm.rs:32:9
|
LL | 0..=2 => println!("0 ... 2"),
| ^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:30:9
--> $DIR/match_overlapping_arm.rs:31:9
|
LL | 2 => println!("2"),
| ^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:54:9
--> $DIR/match_overlapping_arm.rs:55:9
|
LL | 0..11 => println!("0 .. 11"),
| ^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:55:9
--> $DIR/match_overlapping_arm.rs:56:9
|
LL | 0..=11 => println!("0 ... 11"),
| ^^^^^^

error: aborting due to 5 previous errors
error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:61:9
|
LL | 0.. => println!("0 .. 42"),
| ^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:62:9
|
LL | 3.. => println!("3 .. 42"),
| ^^^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:67:9
|
LL | ..=23 => println!("0 ... 23"),
| ^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:68:9
|
LL | ..26 => println!("0 .. 26"),
| ^^^^

error: aborting due to 7 previous errors