diff options
| author | Zack M. Davis <code@zackmdavis.net> | 2017-10-11 23:06:45 -0700 |
|---|---|---|
| committer | Zack M. Davis <code@zackmdavis.net> | 2017-10-16 11:19:18 -0700 |
| commit | f98939c6fd73e0cb776b7ddbc732827b04a4b2a3 (patch) | |
| tree | 17434e00740975940a146dd4961b80b4dc68799c | |
| parent | e596c1d0b803dbdcc91ed57372f26f42ae6d32e8 (diff) | |
| download | rust-f98939c6fd73e0cb776b7ddbc732827b04a4b2a3.tar.gz rust-f98939c6fd73e0cb776b7ddbc732827b04a4b2a3.zip | |
code suggestion for non-shorthand field patterns lint
We also edit the lint description to clarify that this is different from the struct field init shorthand.
| -rw-r--r-- | src/librustc_lint/builtin.rs | 14 | ||||
| -rw-r--r-- | src/libsyntax/codemap.rs | 11 |
2 files changed, 20 insertions, 5 deletions
diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 06bc716f024..5f58e96efba 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -153,7 +153,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxPointers { declare_lint! { NON_SHORTHAND_FIELD_PATTERNS, Warn, - "using `Struct { x: x }` instead of `Struct { x }`" + "using `Struct { x: x }` instead of `Struct { x }` in a pattern" } #[derive(Copy, Clone)] @@ -174,11 +174,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns { } if let PatKind::Binding(_, _, ident, None) = fieldpat.node.pat.node { if ident.node == fieldpat.node.name { - cx.span_lint(NON_SHORTHAND_FIELD_PATTERNS, + let mut err = cx.struct_span_lint(NON_SHORTHAND_FIELD_PATTERNS, fieldpat.span, - &format!("the `{}:` in this pattern is redundant and can \ - be removed", - ident.node)) + &format!("the `{}:` in this pattern is redundant", + ident.node)); + let subspan = cx.tcx.sess.codemap().span_through_char(fieldpat.span, ':'); + err.span_suggestion_short(subspan, + "remove this", + format!("{}", ident.node)); + err.emit(); } } } diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index efaa5e5e3da..dd46903bb88 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -471,6 +471,17 @@ impl CodeMap { } } + /// Given a `Span`, try to get a shorter span ending just after the first + /// occurrence of `char` `c`. + pub fn span_through_char(&self, sp: Span, c: char) -> Span { + if let Ok(snippet) = self.span_to_snippet(sp) { + if let Some(offset) = snippet.find(c) { + return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32)); + } + } + sp + } + pub fn def_span(&self, sp: Span) -> Span { self.span_until_char(sp, '{') } |
