Skip to content

Commit 31b7d64

Browse files
committed
rustc: Add deprecation/renaming support for lints
Since a large number of lints are being renamed for RFC 344, this commit adds some basic deprecation/renaming functionality to the pluggable lint system. It allows a simple mapping of old to new names, and can warn when old names are being used. This change needs to be rolled out in stages. In this commit, the deprecation warning is commented out, but the old name is forwarded to the new one. Once the commit lands and we have generated a new snapshot of the compiler, we can add the deprecation warning and rename all uses of the lints in the rust codebase.
1 parent 3c0d2a7 commit 31b7d64

File tree

1 file changed

+70
-6
lines changed

1 file changed

+70
-6
lines changed

src/librustc/lint/context.rs

+70-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub struct LintStore {
6363
passes: Option<Vec<LintPassObject>>,
6464

6565
/// Lints indexed by name.
66-
by_name: HashMap<String, LintId>,
66+
by_name: HashMap<String, TargetLint>,
6767

6868
/// Current levels of each lint, and where they were set.
6969
levels: HashMap<LintId, LevelSource>,
@@ -73,6 +73,15 @@ pub struct LintStore {
7373
lint_groups: HashMap<&'static str, (Vec<LintId>, bool)>,
7474
}
7575

76+
/// The targed of the `by_name` map, which accounts for renaming/deprecation.
77+
enum TargetLint {
78+
/// A direct lint target
79+
Id(LintId),
80+
81+
/// Temporary renaming, used for easing migration pain; see #16545
82+
Renamed(String, LintId),
83+
}
84+
7685
impl LintStore {
7786
fn get_level_source(&self, lint: LintId) -> LevelSource {
7887
match self.levels.find(&lint) {
@@ -115,7 +124,7 @@ impl LintStore {
115124
self.lints.push((*lint, from_plugin));
116125

117126
let id = LintId::of(*lint);
118-
if !self.by_name.insert(lint.name_lower(), id) {
127+
if !self.by_name.insert(lint.name_lower(), Id(id)) {
119128
let msg = format!("duplicate specification of lint {}", lint.name_lower());
120129
match (sess, from_plugin) {
121130
// We load builtin lints first, so a duplicate is a compiler bug.
@@ -154,6 +163,14 @@ impl LintStore {
154163
}
155164
}
156165

166+
fn register_renamed(&mut self, old_name: &str, new_name: &str) {
167+
let target = match self.by_name.find_equiv(&new_name) {
168+
Some(&Id(lint_id)) => lint_id.clone(),
169+
_ => fail!("invalid lint renaming of {} to {}", old_name, new_name)
170+
};
171+
self.by_name.insert(old_name.to_string(), Renamed(new_name.to_string(), target));
172+
}
173+
157174
pub fn register_builtin(&mut self, sess: Option<&Session>) {
158175
macro_rules! add_builtin ( ( $sess:ident, $($name:ident),*, ) => (
159176
{$(
@@ -208,12 +225,59 @@ impl LintStore {
208225

209226
// We have one lint pass defined in this module.
210227
self.register_pass(sess, false, box GatherNodeLevels as LintPassObject);
228+
229+
// Insert temporary renamings for a one-time deprecation (#16545)
230+
self.register_renamed("unnecessary_typecast", "unused_typecasts");
231+
self.register_renamed("unsigned_negate", "unsigned_negation");
232+
self.register_renamed("type_limits", "unused_comparisons");
233+
self.register_renamed("type_overflow", "overflowing_literals");
234+
self.register_renamed("ctypes", "improper_ctypes");
235+
self.register_renamed("owned_heap_memory", "box_pointers");
236+
self.register_renamed("unused_attribute", "unused_attributes");
237+
self.register_renamed("path_statement", "path_statements");
238+
self.register_renamed("unused_result", "unused_results");
239+
self.register_renamed("non_uppercase_statics", "non_upper_case_globals");
240+
self.register_renamed("unnecessary_parens", "unused_parens");
241+
self.register_renamed("unnecessary_import_braces", "unused_import_braces");
242+
self.register_renamed("unsafe_block", "unsafe_blocks");
243+
self.register_renamed("unnecessary_allocation", "unused_allocation");
244+
self.register_renamed("missing_doc", "missing_docs");
245+
self.register_renamed("unused_extern_crate", "unused_extern_crates");
246+
self.register_renamed("unnecessary_qualification", "unused_qualifications");
247+
self.register_renamed("unrecognized_lint", "unknown_lints");
248+
self.register_renamed("unused_variable", "unused_variables");
249+
self.register_renamed("dead_assignment", "unused_assignments");
250+
self.register_renamed("unknown_crate_type", "unknown_crate_types");
251+
self.register_renamed("variant_size_difference", "variant_size_differences");
252+
self.register_renamed("transmute_fat_ptr", "fat_ptr_transmutes");
253+
254+
}
255+
256+
#[allow(unused_variable)]
257+
fn find_lint(&self, lint_name: &str, sess: &Session, span: Option<Span>)
258+
-> Option<LintId>
259+
{
260+
match self.by_name.find_equiv(&lint_name) {
261+
Some(&Id(lint_id)) => Some(lint_id),
262+
Some(&Renamed(ref new_name, lint_id)) => {
263+
// NOTE(stage0): add the following code after the next snapshot
264+
265+
// let warning = format!("lint {} has been renamed to {}",
266+
// lint_name, new_name);
267+
// match span {
268+
// Some(span) => sess.span_warn(span, warning.as_slice()),
269+
// None => sess.warn(warning.as_slice()),
270+
// };
271+
Some(lint_id)
272+
}
273+
None => None
274+
}
211275
}
212276

213277
pub fn process_command_line(&mut self, sess: &Session) {
214278
for &(ref lint_name, level) in sess.opts.lint_opts.iter() {
215-
match self.by_name.find_equiv(&lint_name.as_slice()) {
216-
Some(&lint_id) => self.set_level(lint_id, (level, CommandLine)),
279+
match self.find_lint(lint_name.as_slice(), sess, None) {
280+
Some(lint_id) => self.set_level(lint_id, (level, CommandLine)),
217281
None => {
218282
match self.lint_groups.iter().map(|(&x, pair)| (x, pair.ref0().clone()))
219283
.collect::<HashMap<&'static str, Vec<LintId>>>()
@@ -421,8 +485,8 @@ impl<'a, 'tcx> Context<'a, 'tcx> {
421485
continue;
422486
}
423487
Ok((lint_name, level, span)) => {
424-
match self.lints.by_name.find_equiv(&lint_name.get()) {
425-
Some(&lint_id) => vec![(lint_id, level, span)],
488+
match self.lints.find_lint(lint_name.get(), &self.tcx.sess, Some(span)) {
489+
Some(lint_id) => vec![(lint_id, level, span)],
426490
None => {
427491
match self.lints.lint_groups.find_equiv(&lint_name.get()) {
428492
Some(&(ref v, _)) => v.iter()

0 commit comments

Comments
 (0)