about summary refs log tree commit diff
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-04-11 19:56:43 +0800
committerGitHub <noreply@github.com>2018-04-11 19:56:43 +0800
commit63c4d50e8adfee19ad5a808bb587ffd738936b11 (patch)
tree4ee5ac8984a8f522f8b3da29bc1f54349b9861ba
parent5ee5de10d2d19eebbca26fbc31b7e49cb51406b1 (diff)
parenta1d90a2a2a2d3ff6843d8fca17be4819dba9ab6b (diff)
downloadrust-63c4d50e8adfee19ad5a808bb587ffd738936b11.tar.gz
rust-63c4d50e8adfee19ad5a808bb587ffd738936b11.zip
Rollup merge of #49614 - zackmdavis:the_phantom_menace, r=petrochenkov
in which the non-shorthand patterns lint keeps its own counsel in macros

In issue #49588, Michael Lamparski pointed out a scenario in which the
non-shorthand-field-patterns lint could be triggered by a macro-expanded
pattern, in a way which was direly unwieldy for the macro author to guard
against and unreasonable to expect the macro user to take into account. We can
avoid this by not linting patterns that come from macro-expansions. Although
this entails accepting "false negatives" where the lint could genuinely improve
macro-templated code, avoiding the reported "true-but-super-annoying positive"
may be worth the trade? (Some precedent for these relative priorities exists as
no. 47775 (5985b0b0).)

Resolves #49588.
-rw-r--r--src/librustc_lint/builtin.rs6
-rw-r--r--src/test/run-pass/issue-49588-non-shorthand-field-patterns-in-pattern-macro.rs24
2 files changed, 30 insertions, 0 deletions
diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs
index 2cc6708bc03..48e9cc498dc 100644
--- a/src/librustc_lint/builtin.rs
+++ b/src/librustc_lint/builtin.rs
@@ -173,6 +173,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
                 }
                 if let PatKind::Binding(_, _, name, None) = fieldpat.node.pat.node {
                     if name.node == fieldpat.node.name {
+                        if let Some(_) = fieldpat.span.ctxt().outer().expn_info() {
+                            // Don't lint if this is a macro expansion: macro authors
+                            // shouldn't have to worry about this kind of style issue
+                            // (Issue #49588)
+                            return;
+                        }
                         let mut err = cx.struct_span_lint(NON_SHORTHAND_FIELD_PATTERNS,
                                      fieldpat.span,
                                      &format!("the `{}:` in this pattern is redundant",
diff --git a/src/test/run-pass/issue-49588-non-shorthand-field-patterns-in-pattern-macro.rs b/src/test/run-pass/issue-49588-non-shorthand-field-patterns-in-pattern-macro.rs
new file mode 100644
index 00000000000..51b2b5a4f7c
--- /dev/null
+++ b/src/test/run-pass/issue-49588-non-shorthand-field-patterns-in-pattern-macro.rs
@@ -0,0 +1,24 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![deny(non_shorthand_field_patterns)]
+
+pub struct Value<A> { pub value: A }
+
+#[macro_export]
+macro_rules! pat {
+    ($a:pat) => {
+        Value { value: $a }
+    };
+}
+
+fn main() {
+    let pat!(value) = Value { value: () };
+}