Supplement DiagnosticBuilderExt with Applicability
This commit is contained in:
parent
92034e20c8
commit
d4c994e670
3 changed files with 20 additions and 11 deletions
|
|
@ -6,6 +6,7 @@ use crate::rustc::hir::*;
|
|||
use crate::syntax::ast::{Attribute, Name};
|
||||
use crate::utils::span_lint_and_then;
|
||||
use crate::utils::sugg::DiagnosticBuilderExt;
|
||||
use crate::rustc_errors::Applicability;
|
||||
|
||||
/// **What it does:** Checks for `#[inline]` on trait methods without bodies
|
||||
///
|
||||
|
|
@ -56,7 +57,7 @@ fn check_attrs(cx: &LateContext<'_, '_>, name: Name, attrs: &[Attribute]) {
|
|||
attr.span,
|
||||
&format!("use of `#[inline]` on trait method `{}` which has no body", name),
|
||||
|db| {
|
||||
db.suggest_remove_item(cx, attr.span, "remove");
|
||||
db.suggest_remove_item(cx, attr.span, "remove", Applicability::Unspecified);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use crate::syntax::source_map::Span;
|
|||
use crate::utils::paths;
|
||||
use crate::utils::{get_trait_def_id, implements_trait, return_ty, same_tys, span_lint_and_then};
|
||||
use crate::utils::sugg::DiagnosticBuilderExt;
|
||||
use crate::rustc_errors::Applicability;
|
||||
|
||||
/// **What it does:** Checks for types with a `fn new() -> Self` method and no
|
||||
/// implementation of
|
||||
|
|
@ -129,7 +130,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
|
|||
impl_item.span,
|
||||
&format!("you should consider deriving a `Default` implementation for `{}`", self_ty),
|
||||
|db| {
|
||||
db.suggest_item_with_attr(cx, sp, "try this", "#[derive(Default)]");
|
||||
db.suggest_item_with_attr(
|
||||
cx,
|
||||
sp,
|
||||
"try this",
|
||||
"#[derive(Default)]",
|
||||
Applicability::Unspecified,
|
||||
);
|
||||
});
|
||||
} else {
|
||||
span_lint_and_then(
|
||||
|
|
@ -143,6 +150,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
|
|||
item.span,
|
||||
"try this",
|
||||
&create_new_without_default_suggest_msg(self_ty),
|
||||
Applicability::Unspecified,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
|
|
|||
|
|
@ -462,7 +462,7 @@ pub trait DiagnosticBuilderExt<'a, T: LintContext<'a>> {
|
|||
/// ```rust,ignore
|
||||
/// db.suggest_item_with_attr(cx, item, "#[derive(Default)]");
|
||||
/// ```
|
||||
fn suggest_item_with_attr<D: Display + ?Sized>(&mut self, cx: &T, item: Span, msg: &str, attr: &D);
|
||||
fn suggest_item_with_attr<D: Display + ?Sized>(&mut self, cx: &T, item: Span, msg: &str, attr: &D, applicability: Applicability);
|
||||
|
||||
/// Suggest to add an item before another.
|
||||
///
|
||||
|
|
@ -476,7 +476,7 @@ pub trait DiagnosticBuilderExt<'a, T: LintContext<'a>> {
|
|||
/// bar();
|
||||
/// }");
|
||||
/// ```
|
||||
fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str);
|
||||
fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str, applicability: Applicability);
|
||||
|
||||
/// Suggest to completely remove an item.
|
||||
///
|
||||
|
|
@ -489,11 +489,11 @@ pub trait DiagnosticBuilderExt<'a, T: LintContext<'a>> {
|
|||
/// ```rust,ignore
|
||||
/// db.suggest_remove_item(cx, item, "remove this")
|
||||
/// ```
|
||||
fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str);
|
||||
fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str, applicability: Applicability);
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_errors::DiagnosticBuilder<'b> {
|
||||
fn suggest_item_with_attr<D: Display + ?Sized>(&mut self, cx: &T, item: Span, msg: &str, attr: &D) {
|
||||
fn suggest_item_with_attr<D: Display + ?Sized>(&mut self, cx: &T, item: Span, msg: &str, attr: &D, applicability: Applicability) {
|
||||
if let Some(indent) = indentation(cx, item) {
|
||||
let span = item.with_hi(item.lo());
|
||||
|
||||
|
|
@ -501,12 +501,12 @@ impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_error
|
|||
span,
|
||||
msg,
|
||||
format!("{}\n{}", attr, indent),
|
||||
Applicability::Unspecified,
|
||||
applicability,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str) {
|
||||
fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str, applicability: Applicability) {
|
||||
if let Some(indent) = indentation(cx, item) {
|
||||
let span = item.with_hi(item.lo());
|
||||
|
||||
|
|
@ -527,12 +527,12 @@ impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_error
|
|||
span,
|
||||
msg,
|
||||
format!("{}\n{}", new_item, indent),
|
||||
Applicability::Unspecified,
|
||||
applicability,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str) {
|
||||
fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str, applicability: Applicability) {
|
||||
let mut remove_span = item;
|
||||
let hi = cx.sess().source_map().next_point(remove_span).hi();
|
||||
let fmpos = cx.sess().source_map().lookup_byte_offset(hi);
|
||||
|
|
@ -549,7 +549,7 @@ impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_error
|
|||
remove_span,
|
||||
msg,
|
||||
String::new(),
|
||||
Applicability::Unspecified,
|
||||
applicability,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue