Skip to content

Commit

Permalink
Rollup merge of rust-lang#53504 - ekse:suggestions-applicability-2, r…
Browse files Browse the repository at this point in the history
…=estebank

Set applicability for more suggestions.

Converts a couple more calls to `span_suggestion_with_applicability`  (rust-lang#50723). To be on the safe side, I marked suggestions that depend on the intent of the user or that are potentially lossy conversions as MaybeIncorrect.

r? @estebank
  • Loading branch information
GuillaumeGomez committed Aug 22, 2018
2 parents 55d9823 + 5a23a0d commit 4fa4bb5
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 125 deletions.
11 changes: 7 additions & 4 deletions src/librustc_borrowck/borrowck/gather_loans/move_error.rs
Expand Up @@ -17,7 +17,7 @@ use rustc::ty;
use rustc_mir::util::borrowck_errors::{BorrowckErrors, Origin};
use syntax::ast;
use syntax_pos;
use errors::DiagnosticBuilder;
use errors::{DiagnosticBuilder, Applicability};
use borrowck::gather_loans::gather_moves::PatternSource;

pub struct MoveErrorCollector<'tcx> {
Expand Down Expand Up @@ -80,9 +80,12 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &[MoveErr
let initializer =
e.init.as_ref().expect("should have an initializer to get an error");
if let Ok(snippet) = bccx.tcx.sess.source_map().span_to_snippet(initializer.span) {
err.span_suggestion(initializer.span,
"consider using a reference instead",
format!("&{}", snippet));
err.span_suggestion_with_applicability(
initializer.span,
"consider using a reference instead",
format!("&{}", snippet),
Applicability::MaybeIncorrect // using a reference may not be the right fix
);
}
}
_ => {
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_passes/ast_validation.rs
Expand Up @@ -25,6 +25,7 @@ use syntax::symbol::keywords;
use syntax::visit::{self, Visitor};
use syntax_pos::Span;
use errors;
use errors::Applicability;

struct AstValidator<'a> {
session: &'a Session,
Expand Down Expand Up @@ -185,11 +186,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
);
match val.node {
ExprKind::Lit(ref v) if v.node.is_numeric() => {
err.span_suggestion(
err.span_suggestion_with_applicability(
place.span.between(val.span),
"if you meant to write a comparison against a negative value, add a \
space in between `<` and `-`",
"< -".to_string(),
Applicability::MaybeIncorrect
);
}
_ => {}
Expand Down
18 changes: 15 additions & 3 deletions src/librustc_resolve/macros.rs
Expand Up @@ -38,6 +38,7 @@ use syntax::symbol::{Symbol, keywords};
use syntax::tokenstream::{TokenStream, TokenTree, Delimited};
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::{Span, DUMMY_SP};
use errors::Applicability;

use std::cell::Cell;
use std::mem;
Expand Down Expand Up @@ -938,9 +939,19 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
if let Some(suggestion) = suggestion {
if suggestion != name {
if let MacroKind::Bang = kind {
err.span_suggestion(span, "you could try the macro", suggestion.to_string());
err.span_suggestion_with_applicability(
span,
"you could try the macro",
suggestion.to_string(),
Applicability::MaybeIncorrect
);
} else {
err.span_suggestion(span, "try", suggestion.to_string());
err.span_suggestion_with_applicability(
span,
"try",
suggestion.to_string(),
Applicability::MaybeIncorrect
);
}
} else {
err.help("have you added the `#[macro_use]` on the module/import?");
Expand Down Expand Up @@ -1065,10 +1076,11 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
if let Some(span) = span {
let found_use = if found_use { "" } else { "\n" };
self.session.struct_span_err(err.use_span, err.warn_msg)
.span_suggestion(
.span_suggestion_with_applicability(
span,
"instead, import the procedural macro like any other item",
format!("use {}::{};{}", err.crate_name, err.name, found_use),
Applicability::MachineApplicable
).emit();
} else {
self.session.struct_span_err(err.use_span, err.warn_msg)
Expand Down
12 changes: 8 additions & 4 deletions src/librustc_typeck/check/callee.rs
Expand Up @@ -20,6 +20,7 @@ use rustc::ty::adjustment::{Adjustment, Adjust, AllowTwoPhase, AutoBorrow, AutoB
use rustc_target::spec::abi;
use syntax::ast::Ident;
use syntax_pos::Span;
use errors::Applicability;

use rustc::hir;

Expand Down Expand Up @@ -234,10 +235,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
err.span_label(call_expr.span, "not a function");

if let Some(ref path) = unit_variant {
err.span_suggestion(call_expr.span,
&format!("`{}` is a unit variant, you need to write it \
without the parenthesis", path),
path.to_string());
err.span_suggestion_with_applicability(
call_expr.span,
&format!("`{}` is a unit variant, you need to write it \
without the parenthesis", path),
path.to_string(),
Applicability::MachineApplicable
);
}

if let hir::ExprKind::Call(ref expr, _) = call_expr.node {
Expand Down

0 comments on commit 4fa4bb5

Please sign in to comment.