Skip to content

Commit bf3f7d5

Browse files
committed
Merge branch 'master' of github.com:rust-lang/rust-clippy
2 parents 2110522 + d420589 commit bf3f7d5

21 files changed

+133
-79
lines changed

clippy_lints/src/attrs.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::reexport::*;
44
use crate::utils::{
5-
in_macro, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg, span_lint_and_then,
6-
without_block_comments,
5+
in_macro, is_present_in_source, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg,
6+
span_lint_and_then, without_block_comments,
77
};
88
use if_chain::if_chain;
99
use rustc::hir::*;
@@ -481,20 +481,6 @@ fn is_word(nmi: &NestedMetaItem, expected: &str) -> bool {
481481
}
482482
}
483483

484-
// If the snippet is empty, it's an attribute that was inserted during macro
485-
// expansion and we want to ignore those, because they could come from external
486-
// sources that the user has no control over.
487-
// For some reason these attributes don't have any expansion info on them, so
488-
// we have to check it this way until there is a better way.
489-
fn is_present_in_source(cx: &LateContext<'_, '_>, span: Span) -> bool {
490-
if let Some(snippet) = snippet_opt(cx, span) {
491-
if snippet.is_empty() {
492-
return false;
493-
}
494-
}
495-
true
496-
}
497-
498484
declare_lint_pass!(DeprecatedCfgAttribute => [DEPRECATED_CFG_ATTR]);
499485

500486
impl EarlyLintPass for DeprecatedCfgAttribute {

clippy_lints/src/enum_variants.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! lint on enum variants that are prefixed or suffixed by the same characters
22
3-
use crate::utils::{camel_case, in_macro};
3+
use crate::utils::{camel_case, in_macro, is_present_in_source};
44
use crate::utils::{span_help_and_lint, span_lint};
55
use rustc::lint::{EarlyContext, EarlyLintPass, Lint, LintArray, LintPass};
66
use rustc::{declare_tool_lint, impl_lint_pass};
@@ -244,7 +244,7 @@ impl EarlyLintPass for EnumVariantNames {
244244
let item_name = item.ident.as_str();
245245
let item_name_chars = item_name.chars().count();
246246
let item_camel = to_camel_case(&item_name);
247-
if !in_macro(item.span) {
247+
if !in_macro(item.span) && is_present_in_source(cx, item.span) {
248248
if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() {
249249
// constants don't have surrounding modules
250250
if !mod_camel.is_empty() {

clippy_lints/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
808808
overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL,
809809
panic_unimplemented::PANIC_PARAMS,
810810
partialeq_ne_impl::PARTIALEQ_NE_IMPL,
811-
path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE,
812811
precedence::PRECEDENCE,
813812
ptr::CMP_NULL,
814813
ptr::MUT_FROM_REF,
@@ -1078,7 +1077,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
10781077
non_copy_const::BORROW_INTERIOR_MUTABLE_CONST,
10791078
non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST,
10801079
open_options::NONSENSICAL_OPEN_OPTIONS,
1081-
path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE,
10821080
ptr::MUT_FROM_REF,
10831081
ranges::ITERATOR_STEP_BY_ZERO,
10841082
regex::INVALID_REGEX,
@@ -1128,6 +1126,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
11281126
missing_const_for_fn::MISSING_CONST_FOR_FN,
11291127
mutex_atomic::MUTEX_INTEGER,
11301128
needless_borrow::NEEDLESS_BORROW,
1129+
path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE,
11311130
redundant_clone::REDUNDANT_CLONE,
11321131
unwrap::PANICKING_UNWRAP,
11331132
unwrap::UNNECESSARY_UNWRAP,

clippy_lints/src/loops.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2393,7 +2393,8 @@ impl<'a, 'tcx> VarCollectorVisitor<'a, 'tcx> {
23932393
Def::Local(node_id) | Def::Upvar(node_id, ..) => {
23942394
self.ids.insert(node_id);
23952395
},
2396-
Def::Static(def_id, mutable) => {
2396+
Def::Static(def_id) => {
2397+
let mutable = self.cx.tcx.is_mutable_static(def_id);
23972398
self.def_ids.insert(def_id, mutable);
23982399
},
23992400
_ => {},

clippy_lints/src/misc.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::consts::{constant, Constant};
1313
use crate::utils::sugg::Sugg;
1414
use crate::utils::{
1515
get_item_name, get_parent_expr, implements_trait, in_constant, in_macro, is_integer_literal, iter_input_pats,
16-
last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint, span_lint_and_then, walk_ptrs_ty,
17-
SpanlessEq,
16+
last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint, span_lint_and_then,
17+
span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq,
1818
};
1919

2020
declare_clippy_lint! {
@@ -282,19 +282,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
282282
if let Some(ref init) = l.init;
283283
then {
284284
if an == BindingAnnotation::Ref || an == BindingAnnotation::RefMut {
285-
let init = Sugg::hir(cx, init, "..");
285+
let sugg_init = Sugg::hir(cx, init, "..");
286286
let (mutopt,initref) = if an == BindingAnnotation::RefMut {
287-
("mut ", init.mut_addr())
287+
("mut ", sugg_init.mut_addr())
288288
} else {
289-
("", init.addr())
289+
("", sugg_init.addr())
290290
};
291291
let tyopt = if let Some(ref ty) = l.ty {
292292
format!(": &{mutopt}{ty}", mutopt=mutopt, ty=snippet(cx, ty.span, "_"))
293293
} else {
294294
String::new()
295295
};
296-
span_lint_and_then(cx,
296+
span_lint_hir_and_then(cx,
297297
TOPLEVEL_REF_ARG,
298+
init.hir_id,
298299
l.pat.span,
299300
"`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead",
300301
|db| {

clippy_lints/src/path_buf_push_overwrite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ declare_clippy_lint! {
3434
/// assert_eq!(x, PathBuf::from("/foo/bar"));
3535
/// ```
3636
pub PATH_BUF_PUSH_OVERWRITE,
37-
correctness,
37+
nursery,
3838
"calling `push` with file system root on `PathBuf` can overwrite it"
3939
}
4040

clippy_lints/src/utils/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ pub fn in_macro(span: Span) -> bool {
9393
span.ctxt().outer().expn_info().is_some()
9494
}
9595

96+
// If the snippet is empty, it's an attribute that was inserted during macro
97+
// expansion and we want to ignore those, because they could come from external
98+
// sources that the user has no control over.
99+
// For some reason these attributes don't have any expansion info on them, so
100+
// we have to check it this way until there is a better way.
101+
pub fn is_present_in_source<'a, T: LintContext<'a>>(cx: &T, span: Span) -> bool {
102+
if let Some(snippet) = snippet_opt(cx, span) {
103+
if snippet.is_empty() {
104+
return false;
105+
}
106+
}
107+
true
108+
}
109+
96110
/// Checks if type is struct, enum or union type with the given def path.
97111
pub fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
98112
match ty.sty {

tests/ui/auxiliary/proc_macro_derive.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ use proc_macro::{quote, TokenStream};
99

1010
#[proc_macro_derive(DeriveSomething)]
1111
pub fn derive(_: TokenStream) -> TokenStream {
12+
// Shound not trigger `used_underscore_binding`
13+
let _inside_derive = 1;
14+
assert_eq!(_inside_derive, _inside_derive);
15+
1216
let output = quote! {
17+
// Should not trigger `useless_attribute`
1318
#[allow(dead_code)]
1419
extern crate clippy_lints;
1520
};

tests/ui/match_as_ref.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
#![allow(unused)]
4+
#![warn(clippy::match_as_ref)]
5+
6+
fn match_as_ref() {
7+
let owned: Option<()> = None;
8+
let borrowed: Option<&()> = owned.as_ref();
9+
10+
let mut mut_owned: Option<()> = None;
11+
let borrow_mut: Option<&mut ()> = mut_owned.as_mut();
12+
}
13+
14+
fn main() {}

tests/ui/match_as_ref.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-rustfix
2+
3+
#![allow(unused)]
4+
#![warn(clippy::match_as_ref)]
5+
6+
fn match_as_ref() {
7+
let owned: Option<()> = None;
8+
let borrowed: Option<&()> = match owned {
9+
None => None,
10+
Some(ref v) => Some(v),
11+
};
12+
13+
let mut mut_owned: Option<()> = None;
14+
let borrow_mut: Option<&mut ()> = match mut_owned {
15+
None => None,
16+
Some(ref mut v) => Some(v),
17+
};
18+
}
19+
20+
fn main() {}

tests/ui/match_as_ref.stderr

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: use as_ref() instead
2+
--> $DIR/match_as_ref.rs:8:33
3+
|
4+
LL | let borrowed: Option<&()> = match owned {
5+
| _________________________________^
6+
LL | | None => None,
7+
LL | | Some(ref v) => Some(v),
8+
LL | | };
9+
| |_____^ help: try this: `owned.as_ref()`
10+
|
11+
= note: `-D clippy::match-as-ref` implied by `-D warnings`
12+
13+
error: use as_mut() instead
14+
--> $DIR/match_as_ref.rs:14:39
15+
|
16+
LL | let borrow_mut: Option<&mut ()> = match mut_owned {
17+
| _______________________________________^
18+
LL | | None => None,
19+
LL | | Some(ref mut v) => Some(v),
20+
LL | | };
21+
| |_____^ help: try this: `mut_owned.as_mut()`
22+
23+
error: aborting due to 2 previous errors
24+

tests/ui/matches.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,6 @@ fn match_wild_err_arm() {
136136
}
137137
}
138138

139-
fn match_as_ref() {
140-
let owned: Option<()> = None;
141-
let borrowed: Option<&()> = match owned {
142-
None => None,
143-
Some(ref v) => Some(v),
144-
};
145-
146-
let mut mut_owned: Option<()> = None;
147-
let borrow_mut: Option<&mut ()> = match mut_owned {
148-
None => None,
149-
Some(ref mut v) => Some(v),
150-
};
151-
}
152-
153139
macro_rules! foo_variant(
154140
($idx:expr) => (Foo::get($idx).unwrap())
155141
);

tests/ui/matches.stderr

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -256,30 +256,8 @@ LL | Ok(3) => println!("ok"),
256256
| ^^^^^^^^^^^^^^
257257
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
258258

259-
error: use as_ref() instead
260-
--> $DIR/matches.rs:141:33
261-
|
262-
LL | let borrowed: Option<&()> = match owned {
263-
| _________________________________^
264-
LL | | None => None,
265-
LL | | Some(ref v) => Some(v),
266-
LL | | };
267-
| |_____^ help: try this: `owned.as_ref()`
268-
|
269-
= note: `-D clippy::match-as-ref` implied by `-D warnings`
270-
271-
error: use as_mut() instead
272-
--> $DIR/matches.rs:147:39
273-
|
274-
LL | let borrow_mut: Option<&mut ()> = match mut_owned {
275-
| _______________________________________^
276-
LL | | None => None,
277-
LL | | Some(ref mut v) => Some(v),
278-
LL | | };
279-
| |_____^ help: try this: `mut_owned.as_mut()`
280-
281259
error: you don't need to add `&` to all patterns
282-
--> $DIR/matches.rs:174:5
260+
--> $DIR/matches.rs:160:5
283261
|
284262
LL | / match foo_variant!(0) {
285263
LL | | &Foo::A => println!("A"),
@@ -292,5 +270,5 @@ LL | match *foo_variant!(0) {
292270
LL | Foo::A => println!("A"),
293271
|
294272

295-
error: aborting due to 20 previous errors
273+
error: aborting due to 18 previous errors
296274

tests/ui/module_name_repetitions.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// compile-flags: --test
2+
13
#![warn(clippy::module_name_repetitions)]
24
#![allow(dead_code)]
35

@@ -13,4 +15,12 @@ mod foo {
1315
pub struct Foobar;
1416
}
1517

18+
#[cfg(test)]
19+
mod test {
20+
#[test]
21+
fn it_works() {
22+
assert_eq!(2 + 2, 4);
23+
}
24+
}
25+
1626
fn main() {}

tests/ui/module_name_repetitions.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: item name starts with its containing module's name
2-
--> $DIR/module_name_repetitions.rs:6:5
2+
--> $DIR/module_name_repetitions.rs:8:5
33
|
44
LL | pub fn foo_bar() {}
55
| ^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::module-name-repetitions` implied by `-D warnings`
88

99
error: item name ends with its containing module's name
10-
--> $DIR/module_name_repetitions.rs:7:5
10+
--> $DIR/module_name_repetitions.rs:9:5
1111
|
1212
LL | pub fn bar_foo() {}
1313
| ^^^^^^^^^^^^^^^^^^^
1414

1515
error: item name starts with its containing module's name
16-
--> $DIR/module_name_repetitions.rs:8:5
16+
--> $DIR/module_name_repetitions.rs:10:5
1717
|
1818
LL | pub struct FooCake {}
1919
| ^^^^^^^^^^^^^^^^^^^^^
2020

2121
error: item name ends with its containing module's name
22-
--> $DIR/module_name_repetitions.rs:9:5
22+
--> $DIR/module_name_repetitions.rs:11:5
2323
|
2424
LL | pub enum CakeFoo {}
2525
| ^^^^^^^^^^^^^^^^^^^
2626

2727
error: item name starts with its containing module's name
28-
--> $DIR/module_name_repetitions.rs:10:5
28+
--> $DIR/module_name_repetitions.rs:12:5
2929
|
3030
LL | pub struct Foo7Bar;
3131
| ^^^^^^^^^^^^^^^^^^^

tests/ui/path_buf_push_overwrite.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-rustfix
22
use std::path::PathBuf;
33

4+
#[warn(clippy::all, clippy::path_buf_push_overwrite)]
45
fn main() {
56
let mut x = PathBuf::from("/foo");
67
x.push("bar");

tests/ui/path_buf_push_overwrite.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-rustfix
22
use std::path::PathBuf;
33

4+
#[warn(clippy::all, clippy::path_buf_push_overwrite)]
45
fn main() {
56
let mut x = PathBuf::from("/foo");
67
x.push("/bar");
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error: Calling `push` with '/' or '/' (file system root) will overwrite the previous path definition
2-
--> $DIR/path_buf_push_overwrite.rs:6:12
2+
--> $DIR/path_buf_push_overwrite.rs:7:12
33
|
44
LL | x.push("/bar");
55
| ^^^^^^ help: try: `"bar"`
66
|
7-
= note: #[deny(clippy::path_buf_push_overwrite)] on by default
7+
= note: `-D clippy::path-buf-push-overwrite` implied by `-D warnings`
88

99
error: aborting due to previous error
1010

tests/ui/toplevel_ref_arg.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ fn main() {
2222

2323
let (ref x, _) = (1, 2); // ok, not top level
2424
println!("The answer is {}.", x);
25+
26+
// Make sure that allowing the lint works
27+
#[allow(clippy::toplevel_ref_arg)]
28+
let ref mut x = 1_234_543;
2529
}

0 commit comments

Comments
 (0)