From 6e63b0dbedd5d4617d95aee1a42b9345b180d27b Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Wed, 1 Aug 2018 20:30:04 -0700 Subject: [PATCH] `Applicability`-ify librustc_lint Andrew Chin recently pointed out (rust-lang/cargo#5846) that it's surprising that `cargo fix` (now shipping with Cargo itself!) doesn't fix very common lint warnings, which is as good of a reminder as any that we should finish #50723. --- src/librustc_lint/builtin.rs | 51 +++++++++++++++---- src/librustc_lint/types.rs | 13 +++-- src/librustc_lint/unused.rs | 10 ++-- .../lint/unused_parens_json_suggestion.stderr | 2 +- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 561f5fbf5c4..be04b326da9 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -84,7 +84,12 @@ fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) { let msg = "denote infinite loops with `loop { ... }`"; let condition_span = cx.tcx.sess.codemap().def_span(e.span); let mut err = cx.struct_span_lint(WHILE_TRUE, condition_span, msg); - err.span_suggestion_short(condition_span, "use `loop`", "loop".to_owned()); + err.span_suggestion_short_with_applicability( + condition_span, + "use `loop`", + "loop".to_owned(), + Applicability::MachineApplicable + ); err.emit(); } } @@ -191,7 +196,12 @@ fn check_pat(&mut self, cx: &LateContext, pat: &hir::Pat) { fieldpat.span, &format!("the `{}:` in this pattern is redundant", ident)); let subspan = cx.tcx.sess.codemap().span_through_char(fieldpat.span, ':'); - err.span_suggestion_short(subspan, "remove this", ident.to_string()); + err.span_suggestion_short_with_applicability( + subspan, + "remove this", + ident.to_string(), + Applicability::MachineApplicable + ); err.emit(); } } @@ -708,10 +718,11 @@ fn check_attribute(&mut self, cx: &EarlyContext, attr: &ast::Attribute) { | "i8" | "i16" | "i32" | "i64" | "i128" | "isize" => { // if the literal could have been a valid `repr` arg, // suggest the correct syntax - warn.span_suggestion( + warn.span_suggestion_with_applicability( attr.span, "give `repr` a hint", repr_str(&lit.as_str()), + Applicability::MachineApplicable ); suggested = true; } @@ -779,7 +790,12 @@ fn check_attribute(&mut self, cx: &EarlyContext, attr: &ast::Attribute) { let msg = format!("use of deprecated attribute `{}`: {}. See {}", name, reason, link); let mut err = cx.struct_span_lint(DEPRECATED, attr.span, &msg); - err.span_suggestion_short(attr.span, "remove this attribute", "".to_owned()); + err.span_suggestion_short_with_applicability( + attr.span, + "remove this attribute", + "".to_owned(), + Applicability::MachineApplicable + ); err.emit(); } return; @@ -1201,7 +1217,12 @@ fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { } }; if let Some(replacement) = suggestion { - err.span_suggestion(vis.span, "try making it public", replacement); + err.span_suggestion_with_applicability( + vis.span, + "try making it public", + replacement, + Applicability::MachineApplicable + ); } }; @@ -1225,9 +1246,14 @@ fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { it.span, "functions generic over \ types must be mangled"); - err.span_suggestion_short(no_mangle_attr.span, - "remove this attribute", - "".to_owned()); + err.span_suggestion_short_with_applicability( + no_mangle_attr.span, + "remove this attribute", + "".to_owned(), + // Use of `#[no_mangle]` suggests FFI intent; correct + // fix may be to monomorphize source by hand + Applicability::MaybeIncorrect + ); err.emit(); break; } @@ -1257,9 +1283,12 @@ fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { .unwrap_or(0) as u32; // `const` is 5 chars let const_span = it.span.with_hi(BytePos(it.span.lo().0 + start + 5)); - err.span_suggestion(const_span, - "try a static value", - "pub static".to_owned()); + err.span_suggestion_with_applicability( + const_span, + "try a static value", + "pub static".to_owned(), + Applicability::MachineApplicable + ); err.emit(); } } diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs index ad4a4fbff64..4dc66fb8121 100644 --- a/src/librustc_lint/types.rs +++ b/src/librustc_lint/types.rs @@ -22,6 +22,7 @@ use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64}; use syntax::{ast, attr}; +use syntax::errors::Applicability; use rustc_target::spec::abi::Abi; use syntax_pos::Span; use syntax::codemap; @@ -143,9 +144,12 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx hir::Expr) { OVERFLOWING_LITERALS, parent_expr.span, "only u8 can be cast into char"); - err.span_suggestion(parent_expr.span, - &"use a char literal instead", - format!("'\\u{{{:X}}}'", lit_val)); + err.span_suggestion_with_applicability( + parent_expr.span, + &"use a char literal instead", + format!("'\\u{{{:X}}}'", lit_val), + Applicability::MachineApplicable + ); err.emit(); return } @@ -398,10 +402,11 @@ fn report_bin_hex_error( { if let Some(pos) = repr_str.chars().position(|c| c == 'i' || c == 'u') { let (sans_suffix, _) = repr_str.split_at(pos); - err.span_suggestion( + err.span_suggestion_with_applicability( expr.span, &format!("consider using `{}` instead", sugg_ty), format!("{}{}", sans_suffix, sugg_ty), + Applicability::MachineApplicable ); } else { err.help(&format!("consider using `{}` instead", sugg_ty)); diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index da291f56ee4..2b8540ea386 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -17,6 +17,7 @@ use syntax::ast; use syntax::attr; +use syntax::errors::Applicability; use syntax::feature_gate::{BUILTIN_ATTRIBUTES, AttributeType}; use syntax::print::pprust; use syntax::symbol::keywords; @@ -303,9 +304,12 @@ fn check_unused_parens_core(&self, _ => false, } }).to_owned(); - err.span_suggestion_short(value.span, - "remove these parentheses", - parens_removed); + err.span_suggestion_short_with_applicability( + value.span, + "remove these parentheses", + parens_removed, + Applicability::MachineApplicable + ); err.emit(); } } diff --git a/src/test/ui/lint/unused_parens_json_suggestion.stderr b/src/test/ui/lint/unused_parens_json_suggestion.stderr index 2ec0111ff4b..2a9bd2948ff 100644 --- a/src/test/ui/lint/unused_parens_json_suggestion.stderr +++ b/src/test/ui/lint/unused_parens_json_suggestion.stderr @@ -82,7 +82,7 @@ ], "label": null, "suggested_replacement": "1 / (2 + 3)", - "suggestion_applicability": "Unspecified", + "suggestion_applicability": "MachineApplicable", "expansion": null } ],