Skip to content

Commit 97cd495

Browse files
committed
auto merge of #9638 : pnkfelix/rust/fsk-issue7526-attempt-to-catch-nonuc-statics-in-match-patterns, r=alexcrichton
r? anyone Address scariest part of #7526 by adding a new more specific lint (that is set to warn by default, rather than allow).
2 parents 33a5928 + 2461b31 commit 97cd495

File tree

13 files changed

+172
-0
lines changed

13 files changed

+172
-0
lines changed

src/librustc/lib/llvm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// LLVM wrappers are intended to be called from trans,
1212
// which already runs in a #[fixed_stack_segment]
1313
#[allow(cstack)];
14+
#[allow(non_uppercase_pattern_statics)];
1415

1516
use std::c_str::ToCStr;
1617
use std::hashmap::HashMap;

src/librustc/middle/check_match.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ pub fn check_arms(cx: &MatchCheckCtxt, arms: &[Arm]) {
133133
_ => false
134134
}
135135
};
136+
136137
do walk_pat(*pat) |p| {
137138
if pat_matches_nan(p) {
138139
cx.tcx.sess.span_warn(p.span, "unmatchable NaN in pattern, \

src/librustc/middle/lint.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub enum lint {
8383
unrecognized_lint,
8484
non_camel_case_types,
8585
non_uppercase_statics,
86+
non_uppercase_pattern_statics,
8687
type_limits,
8788
unused_unsafe,
8889

@@ -209,6 +210,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
209210
default: allow
210211
}),
211212

213+
("non_uppercase_pattern_statics",
214+
LintSpec {
215+
lint: non_uppercase_pattern_statics,
216+
desc: "static constants in match patterns should be all caps",
217+
default: warn
218+
}),
219+
212220
("managed_heap_memory",
213221
LintSpec {
214222
lint: managed_heap_memory,
@@ -1110,6 +1118,22 @@ fn check_item_non_uppercase_statics(cx: &Context, it: &ast::item) {
11101118
}
11111119
}
11121120

1121+
fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
1122+
// Lint for constants that look like binding identifiers (#7526)
1123+
match (&p.node, cx.tcx.def_map.find(&p.id)) {
1124+
(&ast::PatIdent(_, ref path, _), Some(&ast::DefStatic(_, false))) => {
1125+
// last identifier alone is right choice for this lint.
1126+
let ident = path.segments.last().identifier;
1127+
let s = cx.tcx.sess.str_of(ident);
1128+
if s.iter().any(|c| c.is_lowercase()) {
1129+
cx.span_lint(non_uppercase_pattern_statics, path.span,
1130+
"static constant in pattern should be all caps");
1131+
}
1132+
}
1133+
_ => {}
1134+
}
1135+
}
1136+
11131137
struct UnusedUnsafeLintVisitor { stopping_on_items: bool }
11141138

11151139
impl SubitemStoppableVisitor for UnusedUnsafeLintVisitor {
@@ -1516,6 +1540,11 @@ struct LintCheckVisitor;
15161540

15171541
impl Visitor<@mut Context> for LintCheckVisitor {
15181542

1543+
fn visit_pat(&mut self, p:@ast::Pat, cx: @mut Context) {
1544+
check_pat_non_uppercase_statics(cx, p);
1545+
visit::walk_pat(self, p, cx);
1546+
}
1547+
15191548
fn visit_item(&mut self, it:@ast::item, cx: @mut Context) {
15201549

15211550
do cx.with_lint_attrs(it.attrs) {

src/librustc/middle/trans/cabi_arm.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[allow(non_uppercase_pattern_statics)];
12+
1113
use lib::llvm::{llvm, Integer, Pointer, Float, Double, Struct, Array};
1214
use lib::llvm::{Attribute, StructRetAttribute};
1315
use middle::trans::cabi::{FnType, LLVMType};

src/librustc/middle/trans/cabi_mips.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[allow(non_uppercase_pattern_statics)];
1112

1213
use std::libc::c_uint;
1314
use std::num;

src/librustc/middle/trans/cabi_x86_64.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// The classification code for the x86_64 ABI is taken from the clay language
1212
// https://github.com/jckarter/clay/blob/master/compiler/src/externals.cpp
1313

14+
#[allow(non_uppercase_pattern_statics)];
15+
1416
use lib::llvm::{llvm, Integer, Pointer, Float, Double};
1517
use lib::llvm::{Struct, Array, Attribute};
1618
use lib::llvm::{StructRetAttribute, ByValAttribute};

src/librustc/middle/trans/intrinsic.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[allow(non_uppercase_pattern_statics)];
12+
1113
use back::{abi};
1214
use lib::llvm::{SequentiallyConsistent, Acquire, Release, Xchg};
1315
use lib::llvm::{ValueRef, Pointer};

src/librustc/middle/trans/type_.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[allow(non_uppercase_pattern_statics)];
1112

1213
use lib::llvm::{llvm, TypeRef, Bool, False, True, TypeKind};
1314
use lib::llvm::{Float, Double, X86_FP80, PPC_FP128, FP128};

src/libstd/num/f32.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Operations and constants for `f32`
1212
#[allow(missing_doc)];
1313
#[allow(non_uppercase_statics)];
14+
#[allow(non_uppercase_pattern_statics)];
1415

1516
use default::Default;
1617
use libc::c_int;

src/libstd/num/f64.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
#[allow(missing_doc)];
1414
#[allow(non_uppercase_statics)];
15+
#[allow(non_uppercase_pattern_statics)];
1516

1617
use default::Default;
1718
use libc::c_int;

0 commit comments

Comments
 (0)