Skip to content

Rustup to rustc 1.9.0-nightly (eabfc160f 2016-03-08) #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "clippy"
version = "0.0.48"
version = "0.0.49"
authors = [
"Manish Goregaokar <[email protected]>",
"Andre Bogus <[email protected]>",
Expand Down
37 changes: 23 additions & 14 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::cmp::Ordering;
use syntax::ast::LitKind;
use syntax::codemap::Span;
use utils::{COW_PATH, OPTION_PATH, RESULT_PATH};
use utils::{match_type, snippet, span_lint, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block};
use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block};

/// **What it does:** This lint checks for matches with a single arm where an `if let` will usually suffice.
///
Expand Down Expand Up @@ -309,18 +309,26 @@ fn check_match_ref_pats(cx: &LateContext, ex: &Expr, arms: &[Arm], source: Match
if has_only_ref_pats(arms) {
if let ExprAddrOf(Mutability::MutImmutable, ref inner) = ex.node {
let template = match_template(cx, expr.span, source, "", inner);
span_lint(cx,
MATCH_REF_PATS,
expr.span,
&format!("you don't need to add `&` to both the expression and the patterns: use `{}`",
template));
span_lint_and_then(cx,
MATCH_REF_PATS,
expr.span,
"you don't need to add `&` to both the expression and the patterns",
|db| {
db.span_suggestion(expr.span,
"try",
template);
});
} else {
let template = match_template(cx, expr.span, source, "*", ex);
span_lint(cx,
MATCH_REF_PATS,
expr.span,
&format!("instead of prefixing all patterns with `&`, you can dereference the expression: `{}`",
template));
span_lint_and_then(cx,
MATCH_REF_PATS,
expr.span,
"you don't need to add `&` to all patterns",
|db| {
db.span_suggestion(expr.span,
"instead of prefixing all patterns with `&`, you can dereference the expression",
template);
});
}
}
}
Expand Down Expand Up @@ -435,10 +443,11 @@ fn has_only_ref_pats(arms: &[Arm]) -> bool {
fn match_template(cx: &LateContext, span: Span, source: MatchSource, op: &str, expr: &Expr) -> String {
let expr_snippet = snippet(cx, expr.span, "..");
match source {
MatchSource::Normal => format!("match {}{} {{ ...", op, expr_snippet),
MatchSource::IfLetDesugar { .. } => format!("if let ... = {}{} {{", op, expr_snippet),
MatchSource::WhileLetDesugar => format!("while let ... = {}{} {{", op, expr_snippet),
MatchSource::Normal => format!("match {}{} {{ .. }}", op, expr_snippet),
MatchSource::IfLetDesugar { .. } => format!("if let .. = {}{} {{ .. }}", op, expr_snippet),
MatchSource::WhileLetDesugar => format!("while let .. = {}{} {{ .. }}", op, expr_snippet),
MatchSource::ForLoopDesugar => cx.sess().span_bug(span, "for loop desugared to match with &-patterns!"),
MatchSource::TryDesugar => cx.sess().span_bug(span, "`?` operator desugared to match with &-patterns!")
}
}

Expand Down
25 changes: 20 additions & 5 deletions tests/compile-fail/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ fn match_bool() {
fn ref_pats() {
{
let v = &Some(0);
match v { //~ERROR dereference the expression: `match *v { ...`
match v {
//~^ERROR add `&` to all patterns
//~|HELP instead of
//~|SUGGESTION `match *v { .. }`
&Some(v) => println!("{:?}", v),
&None => println!("none"),
}
Expand All @@ -147,13 +150,19 @@ fn ref_pats() {
}
}
let tup =& (1, 2);
match tup { //~ERROR dereference the expression: `match *tup { ...`
match tup {
//~^ERROR add `&` to all patterns
//~|HELP instead of
//~|SUGGESTION `match *tup { .. }`
&(v, 1) => println!("{}", v),
_ => println!("none"),
}
// special case: using & both in expr and pats
let w = Some(0);
match &w { //~ERROR use `match w { ...`
match &w {
//~^ERROR add `&` to both
//~|HELP try
//~|SUGGESTION `match w { .. }`
&Some(v) => println!("{:?}", v),
&None => println!("none"),
}
Expand All @@ -164,12 +173,18 @@ fn ref_pats() {
}

let a = &Some(0);
if let &None = a { //~ERROR dereference the expression: `if let ... = *a {`
if let &None = a {
//~^ERROR add `&` to all patterns
//~|HELP instead of
//~|SUGGESTION `if let ... = *a { .. }`
println!("none");
}

let b = Some(0);
if let &None = &b { //~ERROR use `if let ... = b {`
if let &None = &b {
//~^ERROR add `&` to both
//~|HELP try
//~|SUGGESTION `if let ... = b { .. }`
println!("none");
}
}
Expand Down