X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fwrite.rs;h=d2493c055a519bf33f7909a93ddcba796a56c69d;hb=03960ebab26a1b1d2d43540f47aca6670bdc1e54;hp=bfa9407285b54ae07f3d1240c593605857e6fbeb;hpb=abddd6c4910ddc02fafba8bdfb60c009f7da65d9;p=rust.git diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index bfa9407285b..d2493c055a5 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -7,13 +7,13 @@ use rustc_ast::ast::{Expr, ExprKind, Impl, Item, ItemKind, MacCall, Path, StrLit, StrStyle}; use rustc_ast::token::{self, LitKind}; use rustc_ast::tokenstream::TokenStream; -use rustc_errors::Applicability; +use rustc_errors::{Applicability, DiagnosticBuilder}; use rustc_lexer::unescape::{self, EscapeError}; -use rustc_lint::{EarlyContext, EarlyLintPass}; +use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_parse::parser; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::symbol::{kw, Symbol}; -use rustc_span::{sym, BytePos, Span, DUMMY_SP}; +use rustc_span::{sym, BytePos, InnerSpan, Span, DUMMY_SP}; declare_clippy_lint! { /// ### What it does @@ -72,7 +72,13 @@ /// application and might forget to remove those prints afterward. /// /// ### Known problems - /// Only catches `print!` and `println!` calls. + /// * Only catches `print!` and `println!` calls. + /// * The lint level is unaffected by crate attributes. The level can still + /// be set for functions, modules and other items. To change the level for + /// the entire crate, please use command line flags. More information and a + /// configuration example can be found in [clippy#6610]. + /// + /// [clippy#6610]: https://github.com/rust-lang/rust-clippy/issues/6610#issuecomment-977120558 /// /// ### Example /// ```rust @@ -94,7 +100,13 @@ /// application and might forget to remove those prints afterward. /// /// ### Known problems - /// Only catches `eprint!` and `eprintln!` calls. + /// * Only catches `eprint!` and `eprintln!` calls. + /// * The lint level is unaffected by crate attributes. The level can still + /// be set for functions, modules and other items. To change the level for + /// the entire crate, please use command line flags. More information and a + /// configuration example can be found in [clippy#6610]. + /// + /// [clippy#6610]: https://github.com/rust-lang/rust-clippy/issues/6610#issuecomment-977120558 /// /// ### Example /// ```rust @@ -278,7 +290,7 @@ fn check_item_post(&mut self, _: &EarlyContext<'_>, _: &Item) { fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) { fn is_build_script(cx: &EarlyContext<'_>) -> bool { // Cargo sets the crate name for build scripts to `build_script_build` - cx.sess + cx.sess() .opts .crate_name .as_ref() @@ -330,8 +342,6 @@ fn is_build_script(cx: &EarlyContext<'_>) -> bool { if let (Some(fmt_str), expr) = self.check_tts(cx, mac.args.inner_tokens(), true) { if fmt_str.symbol == kw::Empty { let mut applicability = Applicability::MachineApplicable; - // FIXME: remove this `#[allow(...)]` once the issue #5822 gets fixed - #[allow(clippy::option_if_let_else)] let suggestion = if let Some(e) = expr { snippet_with_applicability(cx, e.span, "v", &mut applicability) } else { @@ -359,9 +369,9 @@ fn is_build_script(cx: &EarlyContext<'_>) -> bool { /// Return this and a boolean indicating whether it only consisted of a newline. fn newline_span(fmtstr: &StrLit) -> (Span, bool) { let sp = fmtstr.span; - let contents = &fmtstr.symbol.as_str(); + let contents = fmtstr.symbol.as_str(); - if *contents == r"\n" { + if contents == r"\n" { return (sp, true); } @@ -441,7 +451,8 @@ fn push(&mut self, arg: rustc_parse_format::Argument<'_>, span: Span) { } } }, - ArgumentNamed(n) => { + ArgumentNamed(n, _) => { + let n = Symbol::intern(n); if let Some(x) = self.named.iter_mut().find(|x| x.0 == n) { match x.1.as_slice() { // A non-empty format string has been seen already. @@ -472,7 +483,7 @@ fn parse_fmt_string(&self, cx: &EarlyContext<'_>, str_lit: &StrLit) -> Option Some(n as usize), }; - let mut parser = Parser::new(&str_sym, style, snippet_opt(cx, str_lit.span), false, ParseMode::Format); + let mut parser = Parser::new(str_sym, style, snippet_opt(cx, str_lit.span), false, ParseMode::Format); let mut args = SimpleFormatArgs::default(); while let Some(arg) = parser.next() { @@ -483,7 +494,7 @@ fn parse_fmt_string(&self, cx: &EarlyContext<'_>, str_lit: &StrLit) -> Option, str_lit: &StrLit) -> Option(&self, cx: &EarlyContext<'a>, tts: TokenStream, is_write: bool) -> (Option, Option) { - let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, false, None); + let mut parser = parser::Parser::new(&cx.sess().parse_sess, tts, false, None); let expr = if is_write { match parser .parse_expr() .map(rustc_ast::ptr::P::into_inner) - .map_err(|mut e| e.cancel()) + .map_err(DiagnosticBuilder::cancel) { // write!(e, ...) Ok(p) if parser.eat(&token::Comma) => Some(p), @@ -551,7 +561,7 @@ fn check_tts<'a>(&self, cx: &EarlyContext<'a>, tts: TokenStream, is_write: bool) } let comma_span = parser.prev_token.span; - let token_expr = if let Ok(expr) = parser.parse_expr().map_err(|mut err| err.cancel()) { + let token_expr = if let Ok(expr) = parser.parse_expr().map_err(DiagnosticBuilder::cancel) { expr } else { return (Some(fmtstr), None); @@ -569,15 +579,20 @@ fn check_tts<'a>(&self, cx: &EarlyContext<'a>, tts: TokenStream, is_write: bool) }; let replacement: String = match lit.token.kind { - LitKind::Integer | LitKind::Float | LitKind::Err => continue, LitKind::StrRaw(_) | LitKind::ByteStrRaw(_) if matches!(fmtstr.style, StrStyle::Raw(_)) => { - lit.token.symbol.as_str().replace("{", "{{").replace("}", "}}") + lit.token.symbol.as_str().replace('{', "{{").replace('}', "}}") }, LitKind::Str | LitKind::ByteStr if matches!(fmtstr.style, StrStyle::Cooked) => { - lit.token.symbol.as_str().replace("{", "{{").replace("}", "}}") + lit.token.symbol.as_str().replace('{', "{{").replace('}', "}}") }, - LitKind::StrRaw(_) | LitKind::Str | LitKind::ByteStrRaw(_) | LitKind::ByteStr => continue, - LitKind::Byte | LitKind::Char => match &*lit.token.symbol.as_str() { + LitKind::StrRaw(_) + | LitKind::Str + | LitKind::ByteStrRaw(_) + | LitKind::ByteStr + | LitKind::Integer + | LitKind::Float + | LitKind::Err => continue, + LitKind::Byte | LitKind::Char => match lit.token.symbol.as_str() { "\"" if matches!(fmtstr.style, StrStyle::Cooked) => "\\\"", "\"" if matches!(fmtstr.style, StrStyle::Raw(0)) => continue, "\\\\" if matches!(fmtstr.style, StrStyle::Raw(_)) => "\\", @@ -659,7 +674,7 @@ fn check_newlines(fmtstr: &StrLit) -> bool { let mut last_was_cr = false; let mut should_lint = false; - let contents = &fmtstr.symbol.as_str(); + let contents = fmtstr.symbol.as_str(); let mut cb = |r: Range, c: Result| { let c = c.unwrap();