Skip to content

Commit 5186d18

Browse files
committed
Update Clippy
1 parent ddb9a3a commit 5186d18

13 files changed

+121
-19
lines changed

clippy_lints/src/consts.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,12 @@ pub fn miri_to_const(result: &ty::Const<'_>) -> Option<Constant> {
490490
},
491491
ConstValue::Slice { data, start, end } => match result.ty.sty {
492492
ty::Ref(_, tam, _) => match tam.sty {
493-
ty::Str => String::from_utf8(data.bytes[start..end].to_owned())
494-
.ok()
495-
.map(Constant::Str),
493+
ty::Str => String::from_utf8(
494+
data.inspect_with_undef_and_ptr_outside_interpreter(start..end)
495+
.to_owned(),
496+
)
497+
.ok()
498+
.map(Constant::Str),
496499
_ => None,
497500
},
498501
_ => None,

clippy_lints/src/lifetimes.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use syntax::source_map::Span;
99
use syntax::symbol::kw;
1010

1111
use crate::reexport::*;
12-
use crate::utils::{last_path_segment, span_lint};
12+
use crate::utils::{last_path_segment, span_lint, trait_ref_of_method};
1313

1414
declare_clippy_lint! {
1515
/// **What it does:** Checks for lifetime annotations which can be removed by
@@ -60,13 +60,21 @@ declare_lint_pass!(Lifetimes => [NEEDLESS_LIFETIMES, EXTRA_UNUSED_LIFETIMES]);
6060
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Lifetimes {
6161
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
6262
if let ItemKind::Fn(ref decl, _, ref generics, id) = item.node {
63-
check_fn_inner(cx, decl, Some(id), generics, item.span);
63+
check_fn_inner(cx, decl, Some(id), generics, item.span, true);
6464
}
6565
}
6666

6767
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
6868
if let ImplItemKind::Method(ref sig, id) = item.node {
69-
check_fn_inner(cx, &sig.decl, Some(id), &item.generics, item.span);
69+
let report_extra_lifetimes = trait_ref_of_method(cx, item.hir_id).is_none();
70+
check_fn_inner(
71+
cx,
72+
&sig.decl,
73+
Some(id),
74+
&item.generics,
75+
item.span,
76+
report_extra_lifetimes,
77+
);
7078
}
7179
}
7280

@@ -76,7 +84,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Lifetimes {
7684
TraitMethod::Required(_) => None,
7785
TraitMethod::Provided(id) => Some(id),
7886
};
79-
check_fn_inner(cx, &sig.decl, body, &item.generics, item.span);
87+
check_fn_inner(cx, &sig.decl, body, &item.generics, item.span, true);
8088
}
8189
}
8290
}
@@ -95,6 +103,7 @@ fn check_fn_inner<'a, 'tcx>(
95103
body: Option<BodyId>,
96104
generics: &'tcx Generics,
97105
span: Span,
106+
report_extra_lifetimes: bool,
98107
) {
99108
if in_external_macro(cx.sess(), span) || has_where_lifetimes(cx, &generics.where_clause) {
100109
return;
@@ -144,7 +153,9 @@ fn check_fn_inner<'a, 'tcx>(
144153
(or replaced with `'_` if needed by type declaration)",
145154
);
146155
}
147-
report_extra_lifetimes(cx, decl, generics);
156+
if report_extra_lifetimes {
157+
self::report_extra_lifetimes(cx, decl, generics);
158+
}
148159
}
149160

150161
fn could_use_elision<'a, 'tcx>(

clippy_lints/src/loops.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,26 +298,25 @@ declare_clippy_lint! {
298298
/// **What it does:** Checks `for` loops over slices with an explicit counter
299299
/// and suggests the use of `.enumerate()`.
300300
///
301-
/// **Why is it bad?** Not only is the version using `.enumerate()` more
302-
/// readable, the compiler is able to remove bounds checks which can lead to
303-
/// faster code in some instances.
301+
/// **Why is it bad?** Using `.enumerate()` makes the intent more clear,
302+
/// declutters the code and may be faster in some instances.
304303
///
305304
/// **Known problems:** None.
306305
///
307306
/// **Example:**
308307
/// ```rust
309308
/// # let v = vec![1];
310-
/// # fn foo(bar: usize) {}
311309
/// # fn bar(bar: usize, baz: usize) {}
312-
/// for i in 0..v.len() { foo(v[i]); }
313-
/// for i in 0..v.len() { bar(i, v[i]); }
310+
/// let mut i = 0;
311+
/// for item in &v {
312+
/// bar(i, *item);
313+
/// i += 1;
314+
/// }
314315
/// ```
315316
/// Could be written as
316317
/// ```rust
317318
/// # let v = vec![1];
318-
/// # fn foo(bar: usize) {}
319319
/// # fn bar(bar: usize, baz: usize) {}
320-
/// for item in &v { foo(*item); }
321320
/// for (i, item) in v.iter().enumerate() { bar(i, *item); }
322321
/// ```
323322
pub EXPLICIT_COUNTER_LOOP,

clippy_lints/src/misc_early.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ impl MiscEarlyLints {
393393
// The `line!()` macro is compiler built-in and a special case for these lints.
394394
let lit_snip = match snippet_opt(cx, lit.span) {
395395
Some(snip) => {
396-
if snip.contains('!') {
396+
// The snip could be empty in case of expand from procedure macro
397+
if snip.is_empty() || snip.contains('!') {
397398
return;
398399
}
399400
snip

clippy_lints/src/utils/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool {
8888
node: ItemKind::Fn(_, header, ..),
8989
..
9090
}) => header.constness == Constness::Const,
91+
Node::ImplItem(&ImplItem {
92+
node: ImplItemKind::Method(ref sig, _),
93+
..
94+
}) => sig.header.constness == Constness::Const,
9195
_ => false,
9296
}
9397
}

tests/ui/cast_lossless_float.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,14 @@ fn main() {
3232
const fn abc(input: f32) -> f64 {
3333
input as f64
3434
}
35+
36+
// Same as the above issue. We can't suggest `::from` in const fns in impls
37+
mod cast_lossless_in_impl {
38+
struct A;
39+
40+
impl A {
41+
pub const fn convert(x: f32) -> f64 {
42+
x as f64
43+
}
44+
}
45+
}

tests/ui/cast_lossless_float.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,14 @@ fn main() {
3232
const fn abc(input: f32) -> f64 {
3333
input as f64
3434
}
35+
36+
// Same as the above issue. We can't suggest `::from` in const fns in impls
37+
mod cast_lossless_in_impl {
38+
struct A;
39+
40+
impl A {
41+
pub const fn convert(x: f32) -> f64 {
42+
x as f64
43+
}
44+
}
45+
}

tests/ui/cast_lossless_integer.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,14 @@ fn main() {
3434
const fn abc(input: u16) -> u32 {
3535
input as u32
3636
}
37+
38+
// Same as the above issue. We can't suggest `::from` in const fns in impls
39+
mod cast_lossless_in_impl {
40+
struct A;
41+
42+
impl A {
43+
pub const fn convert(x: u32) -> u64 {
44+
x as u64
45+
}
46+
}
47+
}

tests/ui/cast_lossless_integer.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,14 @@ fn main() {
3434
const fn abc(input: u16) -> u32 {
3535
input as u32
3636
}
37+
38+
// Same as the above issue. We can't suggest `::from` in const fns in impls
39+
mod cast_lossless_in_impl {
40+
struct A;
41+
42+
impl A {
43+
pub const fn convert(x: u32) -> u64 {
44+
x as u64
45+
}
46+
}
47+
}

tests/ui/extra_unused_lifetimes.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,15 @@ impl X {
6161
fn explicit_self_with_lifetime<'a>(self: &'a Self) {}
6262
}
6363

64+
// Methods implementing traits must have matching lifetimes
65+
mod issue4291 {
66+
trait BadTrait {
67+
fn unused_lt<'a>(x: u8) {}
68+
}
69+
70+
impl BadTrait for () {
71+
fn unused_lt<'a>(_x: u8) {}
72+
}
73+
}
74+
6475
fn main() {}

0 commit comments

Comments
 (0)