Skip to content

Commit 183e19a

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents b1abc81 + 457e7f1 commit 183e19a

35 files changed

+1285
-92
lines changed

.travis.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ os:
99

1010
sudo: false
1111

12+
branches:
13+
# Don't build these branches
14+
except:
15+
# Used by bors
16+
- trying.tmp
17+
- staging.tmp
18+
1219
env:
1320
global:
1421
- RUST_BACKTRACE=1
@@ -56,7 +63,8 @@ matrix:
5663
- env: INTEGRATION=chronotope/chrono
5764
- env: INTEGRATION=serde-rs/serde
5865
- env: INTEGRATION=Geal/nom
59-
- env: INTEGRATION=hyperium/hyper
66+
# uncomment once https://github.com/rust-lang/rust/issues/55376 is fixed
67+
# - env: INTEGRATION=hyperium/hyper
6068
allow_failures:
6169
- os: windows
6270
env: BASE_TEST=true

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ All notable changes to this project will be documented in this file.
744744
[`match_same_arms`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#match_same_arms
745745
[`match_wild_err_arm`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#match_wild_err_arm
746746
[`maybe_infinite_iter`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#maybe_infinite_iter
747+
[`mem_discriminant_non_enum`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#mem_discriminant_non_enum
747748
[`mem_forget`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#mem_forget
748749
[`mem_replace_option_with_none`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#mem_replace_option_with_none
749750
[`min_max`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#min_max
@@ -812,6 +813,7 @@ All notable changes to this project will be documented in this file.
812813
[`range_plus_one`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#range_plus_one
813814
[`range_step_by_zero`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#range_step_by_zero
814815
[`range_zip_with_len`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#range_zip_with_len
816+
[`redundant_clone`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#redundant_clone
815817
[`redundant_closure`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#redundant_closure
816818
[`redundant_closure_call`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#redundant_closure_call
817819
[`redundant_field_names`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#redundant_field_names
@@ -893,6 +895,7 @@ All notable changes to this project will be documented in this file.
893895
[`while_immutable_condition`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#while_immutable_condition
894896
[`while_let_loop`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#while_let_loop
895897
[`while_let_on_iterator`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#while_let_on_iterator
898+
[`wildcard_dependencies`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#wildcard_dependencies
896899
[`write_literal`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#write_literal
897900
[`write_with_newline`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#write_with_newline
898901
[`writeln_empty_string`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#writeln_empty_string

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ We are currently in the process of discussing Clippy 1.0 via the RFC process in
99

1010
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
1111

12-
[There are 280 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
12+
[There are 283 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
1313

1414
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1515

appveyor.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ environment:
77
#- TARGET: x86_64-pc-windows-gnu
88
- TARGET: x86_64-pc-windows-msvc
99

10+
branches:
11+
# Don't build these branches
12+
except:
13+
# Used by bors
14+
- trying.tmp
15+
- staging.tmp
16+
1017
install:
1118
- curl -sSf -o rustup-init.exe https://win.rustup.rs/
1219
- rustup-init.exe -y --default-host %TARGET% --default-toolchain nightly

bors.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
status = [
2+
"continuous-integration/travis-ci/push",
3+
"continuous-integration/appveyor/branch"
4+
]

clippy_lints/src/arithmetic.rs

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ declare_clippy_lint! {
5151

5252
#[derive(Copy, Clone, Default)]
5353
pub struct Arithmetic {
54-
span: Option<Span>,
54+
expr_span: Option<Span>,
55+
/// This field is used to check whether expressions are constants, such as in enum discriminants and consts
56+
const_span: Option<Span>,
5557
}
5658

5759
impl LintPass for Arithmetic {
@@ -62,9 +64,15 @@ impl LintPass for Arithmetic {
6264

6365
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
6466
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
65-
if self.span.is_some() {
67+
if self.expr_span.is_some() {
6668
return;
6769
}
70+
71+
if let Some(span) = self.const_span {
72+
if span.contains(expr.span) {
73+
return;
74+
}
75+
}
6876
match expr.node {
6977
hir::ExprKind::Binary(ref op, ref l, ref r) => {
7078
match op.node {
@@ -86,29 +94,60 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
8694
let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
8795
if l_ty.is_integral() && r_ty.is_integral() {
8896
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
89-
self.span = Some(expr.span);
97+
self.expr_span = Some(expr.span);
9098
} else if l_ty.is_floating_point() && r_ty.is_floating_point() {
9199
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
92-
self.span = Some(expr.span);
100+
self.expr_span = Some(expr.span);
93101
}
94102
},
95103
hir::ExprKind::Unary(hir::UnOp::UnNeg, ref arg) => {
96104
let ty = cx.tables.expr_ty(arg);
97105
if ty.is_integral() {
98106
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
99-
self.span = Some(expr.span);
107+
self.expr_span = Some(expr.span);
100108
} else if ty.is_floating_point() {
101109
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
102-
self.span = Some(expr.span);
110+
self.expr_span = Some(expr.span);
103111
}
104112
},
105113
_ => (),
106114
}
107115
}
108116

109117
fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
110-
if Some(expr.span) == self.span {
111-
self.span = None;
118+
if Some(expr.span) == self.expr_span {
119+
self.expr_span = None;
120+
}
121+
}
122+
123+
fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
124+
let body_owner = cx.tcx.hir.body_owner(body.id());
125+
126+
match cx.tcx.hir.body_owner_kind(body_owner) {
127+
hir::BodyOwnerKind::Static(_)
128+
| hir::BodyOwnerKind::Const => {
129+
let body_span = cx.tcx.hir.span(body_owner);
130+
131+
if let Some(span) = self.const_span {
132+
if span.contains(body_span) {
133+
return;
134+
}
135+
}
136+
self.const_span = Some(body_span);
137+
}
138+
hir::BodyOwnerKind::Fn => (),
139+
}
140+
}
141+
142+
fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
143+
let body_owner = cx.tcx.hir.body_owner(body.id());
144+
let body_span = cx.tcx.hir.span(body_owner);
145+
146+
if let Some(span) = self.const_span {
147+
if span.contains(body_span) {
148+
return;
149+
}
112150
}
151+
self.const_span = None;
113152
}
114153
}

clippy_lints/src/indexing_slicing.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,40 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
108108
if let ExprKind::Index(ref array, ref index) = &expr.node {
109109
let ty = cx.tables.expr_ty(array);
110110
if let Some(range) = higher::range(cx, index) {
111+
111112
// Ranged indexes, i.e. &x[n..m], &x[n..], &x[..n] and &x[..]
112113
if let ty::Array(_, s) = ty.sty {
113114
let size: u128 = s.assert_usize(cx.tcx).unwrap().into();
114-
// Index is a constant range.
115-
if let Some((start, end)) = to_const_range(cx, range, size) {
116-
if start > size || end > size {
115+
116+
let const_range = to_const_range(cx, range, size);
117+
118+
if let (Some(start), _) = const_range {
119+
if start > size {
117120
utils::span_lint(
118121
cx,
119122
OUT_OF_BOUNDS_INDEXING,
120-
expr.span,
123+
range.start.map_or(expr.span, |start| start.span),
121124
"range is out of bounds",
122125
);
126+
return;
123127
}
128+
}
129+
130+
if let (_, Some(end)) = const_range {
131+
if end > size {
132+
utils::span_lint(
133+
cx,
134+
OUT_OF_BOUNDS_INDEXING,
135+
range.end.map_or(expr.span, |end| end.span),
136+
"range is out of bounds",
137+
);
138+
return;
139+
}
140+
}
141+
142+
if let (Some(_), Some(_)) = const_range {
143+
// early return because both start and end are constants
144+
// and we have proven above that they are in bounds
124145
return;
125146
}
126147
}
@@ -161,34 +182,34 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
161182
}
162183
}
163184

164-
/// Returns an option containing a tuple with the start and end (exclusive) of
165-
/// the range.
185+
/// Returns a tuple of options with the start and end (exclusive) values of
186+
/// the range. If the start or end is not constant, None is returned.
166187
fn to_const_range<'a, 'tcx>(
167188
cx: &LateContext<'a, 'tcx>,
168189
range: Range<'_>,
169190
array_size: u128,
170-
) -> Option<(u128, u128)> {
191+
) -> (Option<u128>, Option<u128>) {
171192
let s = range
172193
.start
173194
.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
174195
let start = match s {
175-
Some(Some(Constant::Int(x))) => x,
176-
Some(_) => return None,
177-
None => 0,
196+
Some(Some(Constant::Int(x))) => Some(x),
197+
Some(_) => None,
198+
None => Some(0),
178199
};
179200

180201
let e = range
181202
.end
182203
.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
183204
let end = match e {
184205
Some(Some(Constant::Int(x))) => if range.limits == RangeLimits::Closed {
185-
x + 1
206+
Some(x + 1)
186207
} else {
187-
x
208+
Some(x)
188209
},
189-
Some(_) => return None,
190-
None => array_size,
210+
Some(_) => None,
211+
None => Some(array_size),
191212
};
192213

193-
Some((start, end))
214+
(start, end)
194215
}

clippy_lints/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub mod loops;
144144
pub mod map_clone;
145145
pub mod map_unit_fn;
146146
pub mod matches;
147+
pub mod mem_discriminant;
147148
pub mod mem_forget;
148149
pub mod mem_replace;
149150
pub mod methods;
@@ -178,6 +179,7 @@ pub mod ptr;
178179
pub mod ptr_offset_with_cast;
179180
pub mod question_mark;
180181
pub mod ranges;
182+
pub mod redundant_clone;
181183
pub mod redundant_field_names;
182184
pub mod redundant_pattern_matching;
183185
pub mod reference;
@@ -200,6 +202,7 @@ pub mod unused_label;
200202
pub mod unwrap;
201203
pub mod use_self;
202204
pub mod vec;
205+
pub mod wildcard_dependencies;
203206
pub mod write;
204207
pub mod zero_div_zero;
205208
// end lints modules, do not remove this comment, it’s used in `update_lints`
@@ -391,12 +394,13 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
391394
reg.register_early_lint_pass(box int_plus_one::IntPlusOne);
392395
reg.register_late_lint_pass(box overflow_check_conditional::OverflowCheckConditional);
393396
reg.register_late_lint_pass(box unused_label::UnusedLabel);
394-
reg.register_late_lint_pass(box new_without_default::NewWithoutDefault);
397+
reg.register_late_lint_pass(box new_without_default::NewWithoutDefault::default());
395398
reg.register_late_lint_pass(box blacklisted_name::BlackListedName::new(conf.blacklisted_names.clone()));
396399
reg.register_late_lint_pass(box functions::Functions::new(conf.too_many_arguments_threshold));
397400
reg.register_early_lint_pass(box doc::Doc::new(conf.doc_valid_idents.clone()));
398401
reg.register_late_lint_pass(box neg_multiply::NegMultiply);
399402
reg.register_early_lint_pass(box unsafe_removed_from_name::UnsafeNameRemoval);
403+
reg.register_late_lint_pass(box mem_discriminant::MemDiscriminant);
400404
reg.register_late_lint_pass(box mem_forget::MemForget);
401405
reg.register_late_lint_pass(box mem_replace::MemReplace);
402406
reg.register_late_lint_pass(box arithmetic::Arithmetic::default());
@@ -438,6 +442,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
438442
reg.register_late_lint_pass(box question_mark::Pass);
439443
reg.register_late_lint_pass(box suspicious_trait_impl::SuspiciousImpl);
440444
reg.register_early_lint_pass(box multiple_crate_versions::Pass);
445+
reg.register_early_lint_pass(box wildcard_dependencies::Pass);
441446
reg.register_late_lint_pass(box map_unit_fn::Pass);
442447
reg.register_late_lint_pass(box infallible_destructuring_match::Pass);
443448
reg.register_late_lint_pass(box inherent_impl::Pass::default());
@@ -448,6 +453,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
448453
reg.register_late_lint_pass(box indexing_slicing::IndexingSlicing);
449454
reg.register_late_lint_pass(box non_copy_const::NonCopyConst);
450455
reg.register_late_lint_pass(box ptr_offset_with_cast::Pass);
456+
reg.register_late_lint_pass(box redundant_clone::RedundantClone);
451457

452458
reg.register_lint_group("clippy::restriction", Some("clippy_restriction"), vec![
453459
arithmetic::FLOAT_ARITHMETIC,
@@ -610,6 +616,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
610616
matches::MATCH_REF_PATS,
611617
matches::MATCH_WILD_ERR_ARM,
612618
matches::SINGLE_MATCH,
619+
mem_discriminant::MEM_DISCRIMINANT_NON_ENUM,
613620
mem_replace::MEM_REPLACE_OPTION_WITH_NONE,
614621
methods::CHARS_LAST_CMP,
615622
methods::CHARS_NEXT_CMP,
@@ -922,6 +929,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
922929
loops::NEVER_LOOP,
923930
loops::REVERSE_RANGE_LOOP,
924931
loops::WHILE_IMMUTABLE_CONDITION,
932+
mem_discriminant::MEM_DISCRIMINANT_NON_ENUM,
925933
methods::CLONE_DOUBLE_REF,
926934
methods::TEMPORARY_CSTRING_AS_PTR,
927935
minmax::MIN_MAX,
@@ -967,13 +975,15 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
967975

968976
reg.register_lint_group("clippy::cargo", Some("clippy_cargo"), vec![
969977
multiple_crate_versions::MULTIPLE_CRATE_VERSIONS,
978+
wildcard_dependencies::WILDCARD_DEPENDENCIES,
970979
]);
971980

972981
reg.register_lint_group("clippy::nursery", Some("clippy_nursery"), vec![
973982
attrs::EMPTY_LINE_AFTER_OUTER_ATTR,
974983
fallible_impl_from::FALLIBLE_IMPL_FROM,
975984
mutex_atomic::MUTEX_INTEGER,
976985
needless_borrow::NEEDLESS_BORROW,
986+
redundant_clone::REDUNDANT_CLONE,
977987
unwrap::PANICKING_UNWRAP,
978988
unwrap::UNNECESSARY_UNWRAP,
979989
]);

0 commit comments

Comments
 (0)