Skip to content

Commit a1d90a2

Browse files
committed
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 (5985b0b).) Resolves #49588.
1 parent 4b9b70c commit a1d90a2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/librustc_lint/builtin.rs

+6
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
173173
}
174174
if let PatKind::Binding(_, _, name, None) = fieldpat.node.pat.node {
175175
if name.node == fieldpat.node.name {
176+
if let Some(_) = fieldpat.span.ctxt().outer().expn_info() {
177+
// Don't lint if this is a macro expansion: macro authors
178+
// shouldn't have to worry about this kind of style issue
179+
// (Issue #49588)
180+
return;
181+
}
176182
let mut err = cx.struct_span_lint(NON_SHORTHAND_FIELD_PATTERNS,
177183
fieldpat.span,
178184
&format!("the `{}:` in this pattern is redundant",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(non_shorthand_field_patterns)]
12+
13+
pub struct Value<A> { pub value: A }
14+
15+
#[macro_export]
16+
macro_rules! pat {
17+
($a:pat) => {
18+
Value { value: $a }
19+
};
20+
}
21+
22+
fn main() {
23+
let pat!(value) = Value { value: () };
24+
}

0 commit comments

Comments
 (0)