Skip to content

Commit 2c410f6

Browse files
committed
Update Clippy
1 parent 93bb229 commit 2c410f6

13 files changed

+111
-46
lines changed

clippy_lints/src/consts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
474474
pub fn miri_to_const(result: &ty::Const<'_>) -> Option<Constant> {
475475
use rustc::mir::interpret::{ConstValue, Scalar};
476476
match result.val {
477-
ConstValue::Scalar(Scalar::Raw { data: d, .. }) => match result.ty.kind {
477+
ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data: d, .. })) => match result.ty.kind {
478478
ty::Bool => Some(Constant::Bool(d == 1)),
479479
ty::Uint(_) | ty::Int(_) => Some(Constant::Int(d)),
480480
ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(
@@ -492,7 +492,7 @@ pub fn miri_to_const(result: &ty::Const<'_>) -> Option<Constant> {
492492
// FIXME: implement other conversions.
493493
_ => None,
494494
},
495-
ConstValue::Slice { data, start, end } => match result.ty.kind {
495+
ty::ConstKind::Value(ConstValue::Slice { data, start, end }) => match result.ty.kind {
496496
ty::Ref(_, tam, _) => match tam.kind {
497497
ty::Str => String::from_utf8(
498498
data.inspect_with_undef_and_ptr_outside_interpreter(start..end)

clippy_lints/src/functions.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{
22
attrs::is_proc_macro, iter_input_pats, match_def_path, qpath_res, return_ty, snippet, snippet_opt,
3-
span_help_and_lint, span_lint, span_lint_and_then, type_is_unsafe_function,
3+
span_help_and_lint, span_lint, span_lint_and_then, trait_ref_of_method, type_is_unsafe_function,
44
};
55
use matches::matches;
66
use rustc::hir::{self, def::Res, def_id::DefId, intravisit};
@@ -254,7 +254,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
254254
if let Some(attr) = attr {
255255
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
256256
check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
257-
} else if cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) {
257+
} else if cx.access_levels.is_exported(item.hir_id)
258+
&& !is_proc_macro(&item.attrs)
259+
&& trait_ref_of_method(cx, item.hir_id).is_none()
260+
{
258261
check_must_use_candidate(
259262
cx,
260263
&sig.decl,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ extern crate rustc_errors;
2929
#[allow(unused_extern_crates)]
3030
extern crate rustc_index;
3131
#[allow(unused_extern_crates)]
32+
extern crate rustc_lexer;
33+
#[allow(unused_extern_crates)]
3234
extern crate rustc_mir;
3335
#[allow(unused_extern_crates)]
3436
extern crate rustc_parse;

clippy_lints/src/loops.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2194,8 +2194,9 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
21942194
}
21952195
walk_expr(self, expr);
21962196
}
2197+
21972198
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
2198-
NestedVisitorMap::None
2199+
NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir())
21992200
}
22002201
}
22012202

clippy_lints/src/write.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use std::borrow::Cow;
2+
use std::ops::Range;
3+
14
use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then};
25
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
36
use rustc::{declare_lint_pass, declare_tool_lint};
47
use rustc_errors::Applicability;
8+
use rustc_lexer::unescape::{self, EscapeError};
59
use rustc_parse::parser;
6-
use std::borrow::Cow;
710
use syntax::ast::*;
811
use syntax::token;
912
use syntax::tokenstream::TokenStream;
@@ -202,7 +205,7 @@ impl EarlyLintPass for Write {
202205
} else if mac.path == sym!(print) {
203206
span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
204207
if let (Some(fmt_str), _) = check_tts(cx, &mac.tts, false) {
205-
if check_newlines(&fmt_str) {
208+
if check_newlines(&fmt_str.contents, fmt_str.style) {
206209
span_lint_and_then(
207210
cx,
208211
PRINT_WITH_NEWLINE,
@@ -223,7 +226,7 @@ impl EarlyLintPass for Write {
223226
}
224227
} else if mac.path == sym!(write) {
225228
if let (Some(fmt_str), _) = check_tts(cx, &mac.tts, true) {
226-
if check_newlines(&fmt_str) {
229+
if check_newlines(&fmt_str.contents, fmt_str.style) {
227230
span_lint_and_then(
228231
cx,
229232
WRITE_WITH_NEWLINE,
@@ -442,38 +445,31 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (O
442445
}
443446
}
444447

445-
/// Checks if the format string constains a single newline that terminates it.
448+
/// Checks if the format string contains a single newline that terminates it.
446449
///
447450
/// Literal and escaped newlines are both checked (only literal for raw strings).
448-
fn check_newlines(fmt_str: &FmtStr) -> bool {
449-
let s = &fmt_str.contents;
451+
fn check_newlines(contents: &str, style: StrStyle) -> bool {
452+
let mut has_internal_newline = false;
453+
let mut last_was_cr = false;
454+
let mut should_lint = false;
450455

451-
if s.ends_with('\n') {
452-
return true;
453-
} else if let StrStyle::Raw(_) = fmt_str.style {
454-
return false;
455-
}
456-
457-
if s.len() < 2 {
458-
return false;
459-
}
456+
let mut cb = |r: Range<usize>, c: Result<char, EscapeError>| {
457+
let c = c.unwrap();
460458

461-
let bytes = s.as_bytes();
462-
if bytes[bytes.len() - 2] != b'\\' || bytes[bytes.len() - 1] != b'n' {
463-
return false;
464-
}
465-
466-
let mut escaping = false;
467-
for (index, &byte) in bytes.iter().enumerate() {
468-
if escaping {
469-
if byte == b'n' {
470-
return index == bytes.len() - 1;
459+
if r.end == contents.len() && c == '\n' && !last_was_cr && !has_internal_newline {
460+
should_lint = true;
461+
} else {
462+
last_was_cr = c == '\r';
463+
if c == '\n' {
464+
has_internal_newline = true;
471465
}
472-
escaping = false;
473-
} else if byte == b'\\' {
474-
escaping = true;
475466
}
467+
};
468+
469+
match style {
470+
StrStyle::Cooked => unescape::unescape_str(contents, &mut cb),
471+
StrStyle::Raw(_) => unescape::unescape_raw_str(contents, &mut cb),
476472
}
477473

478-
false
474+
should_lint
479475
}

src/driver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,14 @@ pub fn main() {
268268

269269
// Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
270270
// We're invoking the compiler programmatically, so we ignore this/
271-
let wrapper_mode = Path::new(&orig_args[1]).file_stem() == Some("rustc".as_ref());
271+
let wrapper_mode = orig_args.get(1).map(Path::new).and_then(Path::file_stem) == Some("rustc".as_ref());
272272

273273
if wrapper_mode {
274274
// we still want to be able to invoke it normally though
275275
orig_args.remove(1);
276276
}
277277

278-
if !wrapper_mode && std::env::args().any(|a| a == "--help" || a == "-h") {
278+
if !wrapper_mode && (orig_args.iter().any(|a| a == "--help" || a == "-h") || orig_args.len() == 1) {
279279
display_help();
280280
exit(0);
281281
}

tests/ui/explicit_counter_loop.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,16 @@ mod issue_1670 {
132132
}
133133
}
134134
}
135+
136+
mod issue_4732 {
137+
pub fn test() {
138+
let slice = &[1, 2, 3];
139+
let mut index = 0;
140+
141+
// should not trigger the lint because the count is used after the loop
142+
for _v in slice {
143+
index += 1
144+
}
145+
let _closure = || println!("index: {}", index);
146+
}
147+
}

tests/ui/must_use_candidates.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub trait MyPureTrait {
2828
}
2929

3030
impl MyPureTrait for MyPure {
31-
#[must_use] fn trait_impl_pure(&self, i: u32) -> u32 {
31+
fn trait_impl_pure(&self, i: u32) -> u32 {
3232
i
3333
}
3434
}

tests/ui/must_use_candidates.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ error: this method could have a `#[must_use]` attribute
1212
LL | pub fn inherent_pure(&self) -> u8 {
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn inherent_pure(&self) -> u8`
1414

15-
error: this method could have a `#[must_use]` attribute
16-
--> $DIR/must_use_candidates.rs:31:5
17-
|
18-
LL | fn trait_impl_pure(&self, i: u32) -> u32 {
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] fn trait_impl_pure(&self, i: u32) -> u32`
20-
2115
error: this function could have a `#[must_use]` attribute
2216
--> $DIR/must_use_candidates.rs:48:1
2317
|
@@ -36,5 +30,5 @@ error: this function could have a `#[must_use]` attribute
3630
LL | pub fn arcd(_x: Arc<u32>) -> bool {
3731
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn arcd(_x: Arc<u32>) -> bool`
3832

39-
error: aborting due to 6 previous errors
33+
error: aborting due to 5 previous errors
4034

tests/ui/print_with_newline.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ fn main() {
4242
r"
4343
"
4444
);
45+
46+
// Don't warn on CRLF (#4208)
47+
print!("\r\n");
48+
print!("foo\r\n");
49+
print!("\\r\n"); //~ ERROR
50+
print!("foo\rbar\n") // ~ ERROR
4551
}

0 commit comments

Comments
 (0)