From 6e63b0dbedd5d4617d95aee1a42b9345b180d27b Mon Sep 17 00:00:00 2001
From: "Zack M. Davis" <code@zackmdavis.net>
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 561f5fbf5c41e..be04b326da94c 100644
--- a/src/librustc_lint/builtin.rs
+++ b/src/librustc_lint/builtin.rs
@@ -84,7 +84,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for WhileTrue {
                         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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
                                      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 @@ impl EarlyLintPass for BadRepr {
                         | "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 @@ impl EarlyLintPass for DeprecatedAttr {
                     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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
                 }
             };
             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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
                                                                   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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
                         .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 ad4a4fbff649c..4dc66fb812144 100644
--- a/src/librustc_lint/types.rs
+++ b/src/librustc_lint/types.rs
@@ -22,6 +22,7 @@ use std::cmp;
 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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
                                                              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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
             {
                 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 da291f56ee4e5..2b8540ea3861a 100644
--- a/src/librustc_lint/unused.rs
+++ b/src/librustc_lint/unused.rs
@@ -17,6 +17,7 @@ use lint::{LintPass, EarlyLintPass, LateLintPass};
 
 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 @@ impl UnusedParens {
                             _ => 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 2ec0111ff4b66..2a9bd2948ffeb 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
         }
       ],