Skip to content

Add a lint for unused type parameters #26684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#![feature(no_std)]
#![no_std]
#![allow(raw_pointer_derive)]
#![allow(unused_type_parameters)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems weird that this is necessary? Where are things unused?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One example is core::intrinsics::size_of<T>.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, hm. Shouldn't extern fns with type params consider them used? (But, in general, it makes sense that there may be a few things like that.)

#![deny(missing_docs)]

#![feature(intrinsics)]
Expand Down
7 changes: 7 additions & 0 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ declare_lint! {
"imports that are never used"
}

declare_lint! {
pub UNUSED_TYPE_PARAMETERS,
Warn,
"type parameters that are never used"
}

declare_lint! {
pub UNUSED_EXTERN_CRATES,
Allow,
Expand Down Expand Up @@ -120,6 +126,7 @@ impl LintPass for HardwiredLints {
fn get_lints(&self) -> LintArray {
lint_array!(
UNUSED_IMPORTS,
UNUSED_TYPE_PARAMETERS,
UNUSED_EXTERN_CRATES,
UNUSED_QUALIFICATIONS,
UNKNOWN_LINTS,
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
NON_CAMEL_CASE_TYPES, NON_SNAKE_CASE, NON_UPPER_CASE_GLOBALS);

add_lint_group!(sess, "unused",
UNUSED_IMPORTS, UNUSED_VARIABLES, UNUSED_ASSIGNMENTS, DEAD_CODE,
UNUSED_IMPORTS, UNUSED_TYPE_PARAMETERS,
UNUSED_VARIABLES, UNUSED_ASSIGNMENTS, DEAD_CODE,
UNUSED_MUT, UNREACHABLE_CODE, UNUSED_MUST_USE,
UNUSED_UNSAFE, PATH_STATEMENTS);

Expand Down
54 changes: 44 additions & 10 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ use syntax::visit::{self, Visitor};
use std::collections::{HashMap, HashSet};
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::cell::{Cell, RefCell};
use std::default::Default;
use std::fmt;
use std::mem::replace;
use std::rc::{Rc, Weak};
Expand Down Expand Up @@ -363,15 +364,35 @@ enum BareIdentifierPatternResolution {
struct Rib {
bindings: HashMap<Name, DefLike>,
kind: RibKind,
sources: HashMap<Name, (NodeId, Span)>,
used_names: RefCell<HashSet<Name>>,
}

impl Rib {
fn new(kind: RibKind) -> Rib {
Rib {
bindings: HashMap::new(),
kind: kind
kind: kind,
sources: Default::default(),
used_names: Default::default(),
}
}

fn insert(&mut self, name: Name, def: DefLike) {
self.bindings.insert(name, def);
}

fn insert_with_source(&mut self, name: Name, id: NodeId, span: Span, def: DefLike) {
self.bindings.insert(name, def);
self.sources.insert(name, (id, span));
}

fn get(&self, name: Name) -> Option<DefLike> {
self.bindings.get(&name).map(|def| {
self.used_names.borrow_mut().insert(name);
def.clone()
})
}
}

/// The link from a module up to its nearest parent node.
Expand Down Expand Up @@ -1749,7 +1770,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
// FIXME #4950: Try caching?

for (i, rib) in ribs.iter().enumerate().rev() {
if let Some(def_like) = rib.bindings.get(&name).cloned() {
if let Some(def_like) = rib.get(name) {
return self.upvarify(&ribs[i + 1..], def_like, span);
}
}
Expand All @@ -1770,7 +1791,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
return None
}
}
let result = rib.bindings.get(&name).cloned();
let result = rib.get(name);
if result.is_some() {
return result
}
Expand Down Expand Up @@ -1919,7 +1940,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
{
match type_parameters {
HasTypeParameters(generics, space, rib_kind) => {
let mut function_type_rib = Rib::new(rib_kind);
let mut type_rib = Rib::new(rib_kind);
let mut seen_bindings = HashSet::new();
for (index, type_parameter) in generics.ty_params.iter().enumerate() {
let name = type_parameter.ident.name;
Expand All @@ -1936,13 +1957,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
seen_bindings.insert(name);

// plain insert (no renaming)
function_type_rib.bindings.insert(name,
type_rib.insert_with_source(name, type_parameter.id, type_parameter.span,
DlDef(DefTyParam(space,
index as u32,
local_def(type_parameter.id),
name)));
}
self.type_ribs.push(function_type_rib);
self.type_ribs.push(type_rib);
}

NoTypeParameters => {
Expand All @@ -1953,7 +1974,20 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
f(self);

match type_parameters {
HasTypeParameters(..) => { self.type_ribs.pop(); }
HasTypeParameters(..) => {
{
let type_rib = self.type_ribs.last().unwrap();
for (&name, &(id, span)) in &type_rib.sources {
if !type_rib.used_names.borrow().contains(&name) {
self.session.add_lint(lint::builtin::UNUSED_TYPE_PARAMETERS,
id, span,
"unused type parameter".to_string());
}
}
}

self.type_ribs.pop();
}
NoTypeParameters => { }
}
}
Expand Down Expand Up @@ -2099,7 +2133,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {

// plain insert (no renaming, types are not currently hygienic....)
let name = special_names::type_self;
self_type_rib.bindings.insert(name, DlDef(self_def));
self_type_rib.insert(name, DlDef(self_def));
self.type_ribs.push(self_type_rib);
f(self);
self.type_ribs.pop();
Expand Down Expand Up @@ -2474,7 +2508,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if !bindings_list.contains_key(&renamed) {
let this = &mut *self;
let last_rib = this.value_ribs.last_mut().unwrap();
last_rib.bindings.insert(renamed, DlDef(def));
last_rib.insert(renamed, DlDef(def));
bindings_list.insert(renamed, pat_id);
} else if mode == ArgumentIrrefutableMode &&
bindings_list.contains_key(&renamed) {
Expand Down Expand Up @@ -3409,7 +3443,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
{
let rib = this.label_ribs.last_mut().unwrap();
let renamed = mtwt::resolve(label);
rib.bindings.insert(renamed, def_like);
rib.insert(renamed, def_like);
}

visit::walk_expr(this, expr);
Expand Down
1 change: 1 addition & 0 deletions src/libstd/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ impl error::Error for Error {
}
}

#[allow(unused_type_parameters)]
fn _assert_error_is_sync_send() {
fn _is_sync_send<T: Sync+Send>() {}
_is_sync_send::<Error>();
Expand Down
1 change: 1 addition & 0 deletions src/libstd/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ impl<'a, T: Send + 'a> Drop for JoinGuard<'a, T> {
}
}

#[allow(unused_type_parameters)]
fn _assert_sync_and_send() {
fn _assert_both<T: Send + Sync>() {}
_assert_both::<JoinHandle<()>>();
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/lint-dead-code-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ enum used_enum {
bar3 //~ ERROR variant is never used
}

#[allow(unused_type_parameters)]
fn f<T>() {}

pub fn pub_fn() {
Expand Down
42 changes: 42 additions & 0 deletions src/test/compile-fail/lint-unused-type-parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2015 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(unused_type_parameters)]

pub fn f<T>() {} //~ ERROR unused type parameter

pub struct S;
impl S {
pub fn m<T>() {} //~ ERROR unused type parameter
}

trait Trait {
fn m<T>() {} //~ ERROR unused type parameter
}

// Now test allow attributes

pub mod allow {
#[allow(unused_type_parameters)]
pub fn f<T>() {}

pub struct S;
impl S {
#[allow(unused_type_parameters)]
pub fn m<T>() {}
}

trait Trait {
#[allow(unused_type_parameters)]
fn m<T>() {}
}
}

fn main() {}
1 change: 1 addition & 0 deletions src/test/compile-fail/object-safety-phantom-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#![feature(rustc_attrs)]
#![allow(dead_code)]
#![allow(unused_type_parameters)]

trait Baz {
}
Expand Down