2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use driver::session;
|
2012-09-04 13:54:36 -05:00
|
|
|
use middle::ty;
|
2013-04-12 00:09:54 -05:00
|
|
|
use middle::pat_util;
|
2012-09-04 13:54:36 -05:00
|
|
|
use util::ppaux::{ty_to_str};
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::cmp;
|
|
|
|
use std::hashmap::HashMap;
|
|
|
|
use std::i16;
|
|
|
|
use std::i32;
|
|
|
|
use std::i64;
|
|
|
|
use std::i8;
|
|
|
|
use std::u16;
|
|
|
|
use std::u32;
|
|
|
|
use std::u64;
|
|
|
|
use std::u8;
|
2013-05-18 14:39:17 -05:00
|
|
|
use extra::smallintmap::SmallIntMap;
|
2013-07-19 20:42:11 -05:00
|
|
|
use syntax::ast_map;
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::attr;
|
2013-07-19 06:51:37 -05:00
|
|
|
use syntax::attr::AttrMetaMethods;
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::codemap::span;
|
2013-01-30 11:56:33 -06:00
|
|
|
use syntax::codemap;
|
2013-07-19 20:42:11 -05:00
|
|
|
use syntax::parse::token;
|
|
|
|
use syntax::{ast, oldvisit, ast_util, visit};
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* A 'lint' check is a kind of miscellaneous constraint that a user _might_
|
|
|
|
* want to enforce, but might reasonably want to permit as well, on a
|
|
|
|
* module-by-module basis. They contrast with static constraints enforced by
|
|
|
|
* other phases of the compiler, which are generally required to hold in order
|
|
|
|
* to compile the program at all.
|
|
|
|
*
|
2013-05-07 01:07:00 -05:00
|
|
|
* The lint checking is all consolidated into one pass which runs just before
|
|
|
|
* translation to LLVM bytecode. Throughout compilation, lint warnings can be
|
|
|
|
* added via the `add_lint` method on the Session structure. This requires a
|
|
|
|
* span and an id of the node that the lint is being added to. The lint isn't
|
|
|
|
* actually emitted at that time because it is unknown what the actual lint
|
2013-05-09 12:22:26 -05:00
|
|
|
* level at that location is.
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
2013-05-09 12:22:26 -05:00
|
|
|
* To actually emit lint warnings/errors, a separate pass is used just before
|
|
|
|
* translation. A context keeps track of the current state of all lint levels.
|
|
|
|
* Upon entering a node of the ast which can modify the lint settings, the
|
|
|
|
* previous lint state is pushed onto a stack and the ast is then recursed upon.
|
|
|
|
* As the ast is traversed, this keeps track of the current lint level for all
|
|
|
|
* lint attributes.
|
2013-05-07 01:07:00 -05:00
|
|
|
*
|
2013-05-09 12:22:26 -05:00
|
|
|
* At each node of the ast which can modify lint attributes, all known lint
|
2013-07-19 20:42:11 -05:00
|
|
|
* passes are also applied. Each lint pass is an oldvisit::vt<()> structure.
|
|
|
|
* The visitors are constructed via the lint_*() functions below. There are
|
|
|
|
* also some lint checks which operate directly on ast nodes (such as
|
|
|
|
* @ast::item), and those are organized as check_item_*(). Each visitor added
|
|
|
|
* to the lint context is modified to stop once it reaches a node which could
|
|
|
|
* alter the lint levels. This means that everything is looked at once and
|
|
|
|
* only once by every lint pass.
|
2013-05-09 12:22:26 -05:00
|
|
|
*
|
|
|
|
* With this all in place, to add a new lint warning, all you need to do is to
|
|
|
|
* either invoke `add_lint` on the session at the appropriate time, or write a
|
|
|
|
* lint pass in this module which is just an ast visitor. The context used when
|
|
|
|
* traversing the ast has a `span_lint` method which only needs the span of the
|
|
|
|
* item that's being warned about.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-04-12 19:30:52 -05:00
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2013-01-30 15:44:24 -06:00
|
|
|
pub enum lint {
|
2012-01-19 19:56:05 -06:00
|
|
|
ctypes,
|
2012-04-12 19:30:52 -05:00
|
|
|
unused_imports,
|
2013-07-08 10:34:28 -05:00
|
|
|
unnecessary_qualification,
|
2012-04-26 15:47:13 -05:00
|
|
|
while_true,
|
|
|
|
path_statement,
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 19:08:21 -05:00
|
|
|
unrecognized_lint,
|
2012-08-24 13:04:07 -05:00
|
|
|
non_camel_case_types,
|
2013-06-30 22:51:13 -05:00
|
|
|
non_uppercase_statics,
|
2012-10-24 11:21:57 -05:00
|
|
|
type_limits,
|
2013-04-09 00:31:10 -05:00
|
|
|
unused_unsafe,
|
2012-08-24 13:04:07 -05:00
|
|
|
|
2012-08-28 20:25:41 -05:00
|
|
|
managed_heap_memory,
|
|
|
|
owned_heap_memory,
|
|
|
|
heap_memory,
|
|
|
|
|
2013-04-09 12:16:27 -05:00
|
|
|
unused_variable,
|
|
|
|
dead_assignment,
|
2013-04-12 00:09:54 -05:00
|
|
|
unused_mut,
|
2013-05-18 02:07:28 -05:00
|
|
|
unnecessary_allocation,
|
2013-05-24 03:27:31 -05:00
|
|
|
|
2013-05-28 15:44:53 -05:00
|
|
|
missing_doc,
|
2013-05-30 03:03:32 -05:00
|
|
|
unreachable_code,
|
2013-06-15 22:58:11 -05:00
|
|
|
|
|
|
|
warnings,
|
2012-01-19 02:50:51 -06:00
|
|
|
}
|
|
|
|
|
2013-03-14 13:22:51 -05:00
|
|
|
pub fn level_to_str(lv: level) -> &'static str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match lv {
|
2013-01-05 21:33:37 -06:00
|
|
|
allow => "allow",
|
|
|
|
warn => "warn",
|
|
|
|
deny => "deny",
|
|
|
|
forbid => "forbid"
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 19:08:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone, Eq, Ord)]
|
2013-01-30 15:44:24 -06:00
|
|
|
pub enum level {
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 19:08:21 -05:00
|
|
|
allow, warn, deny, forbid
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2013-07-17 01:49:42 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2013-07-16 23:12:16 -05:00
|
|
|
pub struct LintSpec {
|
2013-02-19 01:40:42 -06:00
|
|
|
lint: lint,
|
2013-03-14 13:22:51 -05:00
|
|
|
desc: &'static str,
|
2013-02-19 01:40:42 -06:00
|
|
|
default: level
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2013-07-16 23:12:16 -05:00
|
|
|
impl Ord for LintSpec {
|
|
|
|
fn lt(&self, other: &LintSpec) -> bool { self.default < other.default }
|
|
|
|
}
|
|
|
|
|
2013-06-12 12:02:55 -05:00
|
|
|
pub type LintDict = HashMap<&'static str, LintSpec>;
|
2013-04-30 00:15:17 -05:00
|
|
|
|
|
|
|
enum AttributedNode<'self> {
|
|
|
|
Item(@ast::item),
|
|
|
|
Method(&'self ast::method),
|
2013-07-19 00:38:55 -05:00
|
|
|
Crate(@ast::Crate),
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
2012-04-12 19:30:52 -05:00
|
|
|
|
2013-05-09 12:22:26 -05:00
|
|
|
#[deriving(Eq)]
|
|
|
|
enum LintSource {
|
|
|
|
Node(span),
|
|
|
|
Default,
|
|
|
|
CommandLine
|
|
|
|
}
|
|
|
|
|
2013-05-07 02:28:58 -05:00
|
|
|
static lint_table: &'static [(&'static str, LintSpec)] = &[
|
|
|
|
("ctypes",
|
|
|
|
LintSpec {
|
|
|
|
lint: ctypes,
|
2013-06-28 17:32:26 -05:00
|
|
|
desc: "proper use of std::libc types in foreign modules",
|
2013-05-07 02:28:58 -05:00
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
("unused_imports",
|
|
|
|
LintSpec {
|
|
|
|
lint: unused_imports,
|
|
|
|
desc: "imports that are never used",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
2013-07-08 10:34:28 -05:00
|
|
|
("unnecessary_qualification",
|
|
|
|
LintSpec {
|
|
|
|
lint: unnecessary_qualification,
|
|
|
|
desc: "detects unnecessarily qualified names",
|
|
|
|
default: allow
|
|
|
|
}),
|
|
|
|
|
2013-05-07 02:28:58 -05:00
|
|
|
("while_true",
|
|
|
|
LintSpec {
|
|
|
|
lint: while_true,
|
|
|
|
desc: "suggest using loop { } instead of while(true) { }",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
("path_statement",
|
|
|
|
LintSpec {
|
|
|
|
lint: path_statement,
|
|
|
|
desc: "path statements with no effect",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
("unrecognized_lint",
|
|
|
|
LintSpec {
|
|
|
|
lint: unrecognized_lint,
|
|
|
|
desc: "unrecognized lint attribute",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
("non_camel_case_types",
|
|
|
|
LintSpec {
|
|
|
|
lint: non_camel_case_types,
|
|
|
|
desc: "types, variants and traits should have camel case names",
|
|
|
|
default: allow
|
|
|
|
}),
|
|
|
|
|
2013-06-30 22:51:13 -05:00
|
|
|
("non_uppercase_statics",
|
|
|
|
LintSpec {
|
|
|
|
lint: non_uppercase_statics,
|
|
|
|
desc: "static constants should have uppercase identifiers",
|
2013-07-12 12:35:18 -05:00
|
|
|
default: allow
|
2013-06-30 22:51:13 -05:00
|
|
|
}),
|
|
|
|
|
2013-05-07 02:28:58 -05:00
|
|
|
("managed_heap_memory",
|
|
|
|
LintSpec {
|
|
|
|
lint: managed_heap_memory,
|
|
|
|
desc: "use of managed (@ type) heap memory",
|
|
|
|
default: allow
|
|
|
|
}),
|
|
|
|
|
|
|
|
("owned_heap_memory",
|
|
|
|
LintSpec {
|
|
|
|
lint: owned_heap_memory,
|
|
|
|
desc: "use of owned (~ type) heap memory",
|
|
|
|
default: allow
|
|
|
|
}),
|
|
|
|
|
|
|
|
("heap_memory",
|
|
|
|
LintSpec {
|
|
|
|
lint: heap_memory,
|
|
|
|
desc: "use of any (~ type or @ type) heap memory",
|
|
|
|
default: allow
|
|
|
|
}),
|
|
|
|
|
|
|
|
("type_limits",
|
|
|
|
LintSpec {
|
|
|
|
lint: type_limits,
|
|
|
|
desc: "comparisons made useless by limits of the types involved",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
("unused_unsafe",
|
|
|
|
LintSpec {
|
|
|
|
lint: unused_unsafe,
|
|
|
|
desc: "unnecessary use of an `unsafe` block",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
("unused_variable",
|
|
|
|
LintSpec {
|
|
|
|
lint: unused_variable,
|
|
|
|
desc: "detect variables which are not used in any way",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
("dead_assignment",
|
|
|
|
LintSpec {
|
|
|
|
lint: dead_assignment,
|
|
|
|
desc: "detect assignments that will never be read",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
("unused_mut",
|
|
|
|
LintSpec {
|
|
|
|
lint: unused_mut,
|
|
|
|
desc: "detect mut variables which don't need to be mutable",
|
|
|
|
default: warn
|
|
|
|
}),
|
2013-05-18 02:07:28 -05:00
|
|
|
|
|
|
|
("unnecessary_allocation",
|
|
|
|
LintSpec {
|
|
|
|
lint: unnecessary_allocation,
|
|
|
|
desc: "detects unnecessary allocations that can be eliminated",
|
|
|
|
default: warn
|
|
|
|
}),
|
2013-05-24 03:27:31 -05:00
|
|
|
|
2013-05-28 15:44:53 -05:00
|
|
|
("missing_doc",
|
2013-05-24 03:27:31 -05:00
|
|
|
LintSpec {
|
2013-05-28 15:44:53 -05:00
|
|
|
lint: missing_doc,
|
|
|
|
desc: "detects missing documentation for public members",
|
2013-05-24 03:27:31 -05:00
|
|
|
default: allow
|
|
|
|
}),
|
2013-05-30 03:03:32 -05:00
|
|
|
|
|
|
|
("unreachable_code",
|
|
|
|
LintSpec {
|
|
|
|
lint: unreachable_code,
|
|
|
|
desc: "detects unreachable code",
|
|
|
|
default: warn
|
|
|
|
}),
|
2013-06-15 22:58:11 -05:00
|
|
|
|
|
|
|
("warnings",
|
|
|
|
LintSpec {
|
|
|
|
lint: warnings,
|
|
|
|
desc: "mass-change the level for lints which produce warnings",
|
|
|
|
default: warn
|
|
|
|
}),
|
2013-05-07 02:28:58 -05:00
|
|
|
];
|
|
|
|
|
2012-04-19 20:04:41 -05:00
|
|
|
/*
|
|
|
|
Pass names should not contain a '-', as the compiler normalizes
|
|
|
|
'-' to '_' in command-line flags
|
|
|
|
*/
|
2013-02-19 01:40:42 -06:00
|
|
|
pub fn get_lint_dict() -> LintDict {
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut map = HashMap::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for &(k, v) in lint_table.iter() {
|
2013-06-12 12:02:55 -05:00
|
|
|
map.insert(k, v);
|
2013-03-22 21:26:41 -05:00
|
|
|
}
|
2013-04-30 00:15:17 -05:00
|
|
|
return map;
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
enum AnyVisitor {
|
|
|
|
// This is a pair so every visitor can visit every node. When a lint pass
|
|
|
|
// is registered, another visitor is created which stops at all items
|
|
|
|
// which can alter the attributes of the ast. This "item stopping visitor"
|
|
|
|
// is the second element of the pair, while the original visitor is the
|
|
|
|
// first element. This means that when visiting a node, the original
|
|
|
|
// recursive call can use the original visitor's method, although the
|
|
|
|
// recursing visitor supplied to the method is the item stopping visitor.
|
|
|
|
OldVisitor(oldvisit::vt<@mut Context>, oldvisit::vt<@mut Context>),
|
|
|
|
NewVisitor(@visit::Visitor<()>),
|
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
struct Context {
|
2013-04-30 00:15:17 -05:00
|
|
|
// All known lint modes (string versions)
|
|
|
|
dict: @LintDict,
|
|
|
|
// Current levels of each lint warning
|
2013-05-09 12:22:26 -05:00
|
|
|
curr: SmallIntMap<(level, LintSource)>,
|
2013-04-30 00:15:17 -05:00
|
|
|
// context we're checking in (used to access fields like sess)
|
|
|
|
tcx: ty::ctxt,
|
2013-05-28 15:44:53 -05:00
|
|
|
// Just a simple flag if we're currently recursing into a trait
|
|
|
|
// implementation. This is only used by the lint_missing_doc() pass
|
|
|
|
in_trait_impl: bool,
|
2013-05-28 16:53:43 -05:00
|
|
|
// Another flag for doc lint emissions. Does some parent of the current node
|
|
|
|
// have the doc(hidden) attribute? Treating this as allow(missing_doc) would
|
|
|
|
// play badly with forbid(missing_doc) when it shouldn't.
|
|
|
|
doc_hidden: bool,
|
2013-04-30 00:15:17 -05:00
|
|
|
// When recursing into an attributed node of the ast which modifies lint
|
|
|
|
// levels, this stack keeps track of the previous lint levels of whatever
|
|
|
|
// was modified.
|
2013-05-09 12:22:26 -05:00
|
|
|
lint_stack: ~[(lint, level, LintSource)],
|
2013-05-07 01:07:00 -05:00
|
|
|
// Each of these visitors represents a lint pass. A number of the lint
|
|
|
|
// attributes are registered by adding a visitor to iterate over the ast.
|
|
|
|
// Others operate directly on @ast::item structures (or similar). Finally,
|
|
|
|
// others still are added to the Session object via `add_lint`, and these
|
|
|
|
// are all passed with the lint_session visitor.
|
2013-07-19 20:42:11 -05:00
|
|
|
visitors: ~[AnyVisitor],
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2013-04-30 00:15:17 -05:00
|
|
|
impl Context {
|
2013-02-19 01:40:42 -06:00
|
|
|
fn get_level(&self, lint: lint) -> level {
|
2013-04-30 00:15:17 -05:00
|
|
|
match self.curr.find(&(lint as uint)) {
|
2013-05-09 12:22:26 -05:00
|
|
|
Some(&(lvl, _)) => lvl,
|
2013-04-30 00:15:17 -05:00
|
|
|
None => allow
|
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2013-05-09 12:22:26 -05:00
|
|
|
fn get_source(&self, lint: lint) -> LintSource {
|
|
|
|
match self.curr.find(&(lint as uint)) {
|
|
|
|
Some(&(_, src)) => src,
|
|
|
|
None => Default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_level(&mut self, lint: lint, level: level, src: LintSource) {
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 19:08:21 -05:00
|
|
|
if level == allow {
|
2013-02-22 23:37:37 -06:00
|
|
|
self.curr.remove(&(lint as uint));
|
2012-04-12 19:30:52 -05:00
|
|
|
} else {
|
2013-05-09 12:22:26 -05:00
|
|
|
self.curr.insert(lint as uint, (level, src));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 12:02:55 -05:00
|
|
|
fn lint_to_str(&self, lint: lint) -> &'static str {
|
2013-08-03 11:45:23 -05:00
|
|
|
for (k, v) in self.dict.iter() {
|
2013-05-09 12:22:26 -05:00
|
|
|
if v.lint == lint {
|
2013-06-12 12:02:55 -05:00
|
|
|
return *k;
|
2013-05-09 12:22:26 -05:00
|
|
|
}
|
2012-04-12 19:30:52 -05:00
|
|
|
}
|
2013-05-09 12:22:26 -05:00
|
|
|
fail!("unregistered lint %?", lint);
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2013-04-30 00:15:17 -05:00
|
|
|
fn span_lint(&self, lint: lint, span: span, msg: &str) {
|
2013-05-09 12:22:26 -05:00
|
|
|
let (level, src) = match self.curr.find(&(lint as uint)) {
|
2013-06-15 22:58:11 -05:00
|
|
|
None => { return }
|
|
|
|
Some(&(warn, src)) => (self.get_level(warnings), src),
|
2013-05-09 12:22:26 -05:00
|
|
|
Some(&pair) => pair,
|
|
|
|
};
|
2013-06-15 22:58:11 -05:00
|
|
|
if level == allow { return }
|
2013-05-09 12:22:26 -05:00
|
|
|
|
|
|
|
let mut note = None;
|
|
|
|
let msg = match src {
|
|
|
|
Default | CommandLine => {
|
|
|
|
fmt!("%s [-%c %s%s]", msg, match level {
|
|
|
|
warn => 'W', deny => 'D', forbid => 'F',
|
|
|
|
allow => fail!()
|
2013-06-11 06:46:40 -05:00
|
|
|
}, self.lint_to_str(lint).replace("_", "-"),
|
2013-05-09 12:22:26 -05:00
|
|
|
if src == Default { " (default)" } else { "" })
|
|
|
|
},
|
|
|
|
Node(src) => {
|
|
|
|
note = Some(src);
|
|
|
|
msg.to_str()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match level {
|
|
|
|
warn => { self.tcx.sess.span_warn(span, msg); }
|
|
|
|
deny | forbid => { self.tcx.sess.span_err(span, msg); }
|
|
|
|
allow => fail!(),
|
|
|
|
}
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for &span in note.iter() {
|
2013-05-09 12:22:26 -05:00
|
|
|
self.tcx.sess.span_note(span, "lint level defined here");
|
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 19:08:21 -05:00
|
|
|
* Merge the lints specified by any lint attributes into the
|
2012-07-04 16:53:12 -05:00
|
|
|
* current lint context, call the provided function, then reset the
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 19:08:21 -05:00
|
|
|
* lints in effect to their previous state.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2013-07-19 06:51:37 -05:00
|
|
|
fn with_lint_attrs(@mut self, attrs: &[ast::Attribute], f: &fn()) {
|
2013-04-30 00:15:17 -05:00
|
|
|
// Parse all of the lint attributes, and then add them all to the
|
|
|
|
// current dictionary of lint information. Along the way, keep a history
|
|
|
|
// of what we changed so we can roll everything back after invoking the
|
|
|
|
// specified closure
|
|
|
|
let mut pushed = 0u;
|
2013-08-02 01:17:20 -05:00
|
|
|
do each_lint(self.tcx.sess, attrs) |meta, level, lintname| {
|
|
|
|
match self.dict.find_equiv(&lintname) {
|
|
|
|
None => {
|
|
|
|
self.span_lint(
|
|
|
|
unrecognized_lint,
|
|
|
|
meta.span,
|
|
|
|
fmt!("unknown `%s` attribute: `%s`",
|
|
|
|
level_to_str(level), lintname));
|
|
|
|
}
|
|
|
|
Some(lint) => {
|
|
|
|
let lint = lint.lint;
|
|
|
|
let now = self.get_level(lint);
|
|
|
|
if now == forbid && level != forbid {
|
|
|
|
self.tcx.sess.span_err(meta.span,
|
|
|
|
fmt!("%s(%s) overruled by outer forbid(%s)",
|
|
|
|
level_to_str(level),
|
|
|
|
lintname, lintname));
|
|
|
|
} else if now != level {
|
|
|
|
let src = self.get_source(lint);
|
|
|
|
self.lint_stack.push((lint, now, src));
|
|
|
|
pushed += 1;
|
|
|
|
self.set_level(lint, level, Node(meta.span));
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 01:07:00 -05:00
|
|
|
}
|
2013-08-02 01:17:20 -05:00
|
|
|
true
|
|
|
|
};
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2013-05-28 16:53:43 -05:00
|
|
|
// detect doc(hidden)
|
2013-07-19 06:51:37 -05:00
|
|
|
let mut doc_hidden = do attrs.iter().any |attr| {
|
|
|
|
"doc" == attr.name() &&
|
|
|
|
match attr.meta_item_list() {
|
|
|
|
Some(l) => attr::contains_name(l, "hidden"),
|
|
|
|
None => false // not of the form #[doc(...)]
|
2013-05-28 16:53:43 -05:00
|
|
|
}
|
2013-07-19 06:51:37 -05:00
|
|
|
};
|
|
|
|
|
2013-05-28 16:53:43 -05:00
|
|
|
if doc_hidden && !self.doc_hidden {
|
|
|
|
self.doc_hidden = true;
|
|
|
|
} else {
|
|
|
|
doc_hidden = false;
|
|
|
|
}
|
|
|
|
|
2013-04-30 00:15:17 -05:00
|
|
|
f();
|
2012-06-04 18:07:54 -05:00
|
|
|
|
2013-04-30 00:15:17 -05:00
|
|
|
// rollback
|
2013-05-28 16:53:43 -05:00
|
|
|
if doc_hidden && self.doc_hidden {
|
|
|
|
self.doc_hidden = false;
|
|
|
|
}
|
2013-07-31 21:18:19 -05:00
|
|
|
do pushed.times {
|
2013-05-09 12:22:26 -05:00
|
|
|
let (lint, lvl, src) = self.lint_stack.pop();
|
|
|
|
self.set_level(lint, lvl, src);
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
2012-06-04 18:07:54 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
fn add_oldvisit_lint(&mut self, v: oldvisit::vt<@mut Context>) {
|
|
|
|
self.visitors.push(OldVisitor(v, item_stopping_visitor(v)));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_lint(&mut self, v: @visit::Visitor<()>) {
|
|
|
|
self.visitors.push(NewVisitor(v));
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
|
2013-05-27 22:21:29 -05:00
|
|
|
fn process(@mut self, n: AttributedNode) {
|
2013-05-28 15:44:53 -05:00
|
|
|
// see comment of the `visitors` field in the struct for why there's a
|
|
|
|
// pair instead of just one visitor.
|
2013-04-30 00:15:17 -05:00
|
|
|
match n {
|
2013-05-07 01:07:00 -05:00
|
|
|
Item(it) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for visitor in self.visitors.iter() {
|
2013-07-19 20:42:11 -05:00
|
|
|
match *visitor {
|
|
|
|
OldVisitor(orig, stopping) => {
|
|
|
|
(orig.visit_item)(it, (self, stopping));
|
|
|
|
}
|
|
|
|
NewVisitor(new_visitor) => {
|
|
|
|
new_visitor.visit_item(it, ());
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 01:07:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Crate(c) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for visitor in self.visitors.iter() {
|
2013-07-19 20:42:11 -05:00
|
|
|
match *visitor {
|
|
|
|
OldVisitor(_, stopping) => {
|
|
|
|
oldvisit::visit_crate(c, (self, stopping))
|
|
|
|
}
|
|
|
|
NewVisitor(new_visitor) => {
|
|
|
|
visit::visit_crate(new_visitor, c, ())
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 01:07:00 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
// Can't use oldvisit::visit_method_helper because the
|
2013-04-30 00:15:17 -05:00
|
|
|
// item_stopping_visitor has overridden visit_fn(&fk_method(... ))
|
|
|
|
// to be a no-op, so manually invoke visit_fn.
|
2013-05-07 01:07:00 -05:00
|
|
|
Method(m) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for visitor in self.visitors.iter() {
|
2013-07-19 20:42:11 -05:00
|
|
|
match *visitor {
|
|
|
|
OldVisitor(orig, stopping) => {
|
|
|
|
let fk = oldvisit::fk_method(m.ident,
|
|
|
|
&m.generics,
|
|
|
|
m);
|
|
|
|
(orig.visit_fn)(&fk,
|
|
|
|
&m.decl,
|
|
|
|
&m.body,
|
|
|
|
m.span,
|
|
|
|
m.id,
|
|
|
|
(self, stopping));
|
|
|
|
}
|
|
|
|
NewVisitor(new_visitor) => {
|
|
|
|
let fk = visit::fk_method(m.ident,
|
|
|
|
&m.generics,
|
|
|
|
m);
|
|
|
|
new_visitor.visit_fn(&fk,
|
|
|
|
&m.decl,
|
|
|
|
&m.body,
|
|
|
|
m.span,
|
|
|
|
m.id,
|
|
|
|
())
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 01:07:00 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-04 18:07:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 01:07:00 -05:00
|
|
|
pub fn each_lint(sess: session::Session,
|
2013-07-19 06:51:37 -05:00
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
f: &fn(@ast::MetaItem, level, @str) -> bool) -> bool {
|
2013-06-21 07:29:53 -05:00
|
|
|
let xs = [allow, warn, deny, forbid];
|
2013-08-03 11:45:23 -05:00
|
|
|
for &level in xs.iter() {
|
2013-05-07 01:07:00 -05:00
|
|
|
let level_name = level_to_str(level);
|
2013-08-03 11:45:23 -05:00
|
|
|
for attr in attrs.iter().filter(|m| level_name == m.name()) {
|
2013-05-07 01:07:00 -05:00
|
|
|
let meta = attr.node.value;
|
|
|
|
let metas = match meta.node {
|
2013-07-19 06:51:37 -05:00
|
|
|
ast::MetaList(_, ref metas) => metas,
|
2013-05-07 01:07:00 -05:00
|
|
|
_ => {
|
2013-05-19 00:07:44 -05:00
|
|
|
sess.span_err(meta.span, "malformed lint attribute");
|
2013-05-07 01:07:00 -05:00
|
|
|
loop;
|
|
|
|
}
|
|
|
|
};
|
2013-08-03 11:45:23 -05:00
|
|
|
for meta in metas.iter() {
|
2013-05-07 01:07:00 -05:00
|
|
|
match meta.node {
|
2013-07-19 06:51:37 -05:00
|
|
|
ast::MetaWord(lintname) => {
|
2013-05-07 01:07:00 -05:00
|
|
|
if !f(*meta, level, lintname) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2013-05-19 00:07:44 -05:00
|
|
|
sess.span_err(meta.span, "malformed lint attribute");
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 07:29:53 -05:00
|
|
|
true
|
2012-06-04 18:07:54 -05:00
|
|
|
}
|
|
|
|
|
2012-05-31 20:24:00 -05:00
|
|
|
// Take a visitor, and modify it so that it will not proceed past subitems.
|
|
|
|
// This is used to make the simple visitors used for the lint passes
|
|
|
|
// not traverse into subitems, since that is handled by the outer
|
|
|
|
// lint visitor.
|
2013-07-19 20:42:11 -05:00
|
|
|
fn item_stopping_visitor<E>(outer: oldvisit::vt<E>) -> oldvisit::vt<E> {
|
|
|
|
oldvisit::mk_vt(@oldvisit::Visitor {
|
2013-06-11 21:55:16 -05:00
|
|
|
visit_item: |_i, (_e, _v)| { },
|
|
|
|
visit_fn: |fk, fd, b, s, id, (e, v)| {
|
2013-04-30 00:15:17 -05:00
|
|
|
match *fk {
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::fk_method(*) => {}
|
2013-06-11 21:55:16 -05:00
|
|
|
_ => (outer.visit_fn)(fk, fd, b, s, id, (e, v))
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
},
|
2013-07-08 10:34:28 -05:00
|
|
|
.. **outer})
|
2012-05-31 20:24:00 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
fn lint_while_true() -> oldvisit::vt<@mut Context> {
|
|
|
|
oldvisit::mk_vt(@oldvisit::Visitor {
|
|
|
|
visit_expr: |e,
|
|
|
|
(cx, vt): (@mut Context, oldvisit::vt<@mut Context>)| {
|
2013-04-30 00:15:17 -05:00
|
|
|
match e.node {
|
|
|
|
ast::expr_while(cond, _) => {
|
|
|
|
match cond.node {
|
|
|
|
ast::expr_lit(@codemap::spanned {
|
|
|
|
node: ast::lit_bool(true), _}) =>
|
|
|
|
{
|
|
|
|
cx.span_lint(while_true, e.span,
|
|
|
|
"denote infinite loops with \
|
|
|
|
loop { ... }");
|
2013-01-08 16:00:45 -06:00
|
|
|
}
|
2013-04-30 00:15:17 -05:00
|
|
|
_ => ()
|
2012-04-19 20:04:41 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-30 00:15:17 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_expr(e, (cx, vt));
|
2013-04-30 00:15:17 -05:00
|
|
|
},
|
2013-07-19 20:42:11 -05:00
|
|
|
.. *oldvisit::default_visitor()
|
2013-05-07 01:07:00 -05:00
|
|
|
})
|
2012-10-24 10:20:54 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
fn lint_type_limits() -> oldvisit::vt<@mut Context> {
|
2013-03-22 13:09:13 -05:00
|
|
|
fn is_valid<T:cmp::Ord>(binop: ast::binop, v: T,
|
2012-10-24 11:21:57 -05:00
|
|
|
min: T, max: T) -> bool {
|
|
|
|
match binop {
|
|
|
|
ast::lt => v <= max,
|
|
|
|
ast::le => v < max,
|
|
|
|
ast::gt => v >= min,
|
|
|
|
ast::ge => v > min,
|
|
|
|
ast::eq | ast::ne => v >= min && v <= max,
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!()
|
2012-10-24 11:21:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
fn rev_binop(binop: ast::binop) -> ast::binop {
|
2012-10-24 11:21:57 -05:00
|
|
|
match binop {
|
|
|
|
ast::lt => ast::gt,
|
|
|
|
ast::le => ast::ge,
|
|
|
|
ast::gt => ast::lt,
|
|
|
|
ast::ge => ast::le,
|
|
|
|
_ => binop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-17 10:47:51 -05:00
|
|
|
// for int & uint, be conservative with the warnings, so that the
|
|
|
|
// warnings are consistent between 32- and 64-bit platforms
|
2013-03-22 13:09:13 -05:00
|
|
|
fn int_ty_range(int_ty: ast::int_ty) -> (i64, i64) {
|
2012-10-24 11:21:57 -05:00
|
|
|
match int_ty {
|
2013-05-17 10:47:51 -05:00
|
|
|
ast::ty_i => (i64::min_value, i64::max_value),
|
2012-10-24 11:21:57 -05:00
|
|
|
ast::ty_char => (u32::min_value as i64, u32::max_value as i64),
|
|
|
|
ast::ty_i8 => (i8::min_value as i64, i8::max_value as i64),
|
|
|
|
ast::ty_i16 => (i16::min_value as i64, i16::max_value as i64),
|
|
|
|
ast::ty_i32 => (i32::min_value as i64, i32::max_value as i64),
|
|
|
|
ast::ty_i64 => (i64::min_value, i64::max_value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
fn uint_ty_range(uint_ty: ast::uint_ty) -> (u64, u64) {
|
2012-10-24 11:21:57 -05:00
|
|
|
match uint_ty {
|
2013-05-17 10:47:51 -05:00
|
|
|
ast::ty_u => (u64::min_value, u64::max_value),
|
2012-10-24 11:21:57 -05:00
|
|
|
ast::ty_u8 => (u8::min_value as u64, u8::max_value as u64),
|
|
|
|
ast::ty_u16 => (u16::min_value as u64, u16::max_value as u64),
|
|
|
|
ast::ty_u32 => (u32::min_value as u64, u32::max_value as u64),
|
|
|
|
ast::ty_u64 => (u64::min_value, u64::max_value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
fn check_limits(cx: &Context,
|
|
|
|
binop: ast::binop,
|
|
|
|
l: @ast::expr,
|
|
|
|
r: @ast::expr)
|
|
|
|
-> bool {
|
2013-01-10 12:59:58 -06:00
|
|
|
let (lit, expr, swap) = match (&l.node, &r.node) {
|
|
|
|
(&ast::expr_lit(_), _) => (l, r, true),
|
|
|
|
(_, &ast::expr_lit(_)) => (r, l, false),
|
2012-10-24 11:21:57 -05:00
|
|
|
_ => return true
|
|
|
|
};
|
|
|
|
// Normalize the binop so that the literal is always on the RHS in
|
|
|
|
// the comparison
|
2013-04-30 00:15:17 -05:00
|
|
|
let norm_binop = if swap {
|
2012-10-24 11:21:57 -05:00
|
|
|
rev_binop(binop)
|
|
|
|
} else {
|
|
|
|
binop
|
|
|
|
};
|
2013-07-02 14:47:32 -05:00
|
|
|
match ty::get(ty::expr_ty(cx.tcx, expr)).sty {
|
2012-10-24 11:21:57 -05:00
|
|
|
ty::ty_int(int_ty) => {
|
|
|
|
let (min, max) = int_ty_range(int_ty);
|
|
|
|
let lit_val: i64 = match lit.node {
|
|
|
|
ast::expr_lit(@li) => match li.node {
|
|
|
|
ast::lit_int(v, _) => v,
|
|
|
|
ast::lit_uint(v, _) => v as i64,
|
|
|
|
ast::lit_int_unsuffixed(v) => v,
|
|
|
|
_ => return true
|
|
|
|
},
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!()
|
2012-10-24 11:21:57 -05:00
|
|
|
};
|
|
|
|
is_valid(norm_binop, lit_val, min, max)
|
|
|
|
}
|
|
|
|
ty::ty_uint(uint_ty) => {
|
|
|
|
let (min, max): (u64, u64) = uint_ty_range(uint_ty);
|
|
|
|
let lit_val: u64 = match lit.node {
|
|
|
|
ast::expr_lit(@li) => match li.node {
|
|
|
|
ast::lit_int(v, _) => v as u64,
|
|
|
|
ast::lit_uint(v, _) => v,
|
|
|
|
ast::lit_int_unsuffixed(v) => v as u64,
|
|
|
|
_ => return true
|
|
|
|
},
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!()
|
2012-10-24 11:21:57 -05:00
|
|
|
};
|
|
|
|
is_valid(norm_binop, lit_val, min, max)
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
fn is_comparison(binop: ast::binop) -> bool {
|
2012-10-24 11:21:57 -05:00
|
|
|
match binop {
|
|
|
|
ast::eq | ast::lt | ast::le |
|
|
|
|
ast::ne | ast::ge | ast::gt => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::mk_vt(@oldvisit::Visitor {
|
|
|
|
visit_expr: |e,
|
|
|
|
(cx, vt): (@mut Context, oldvisit::vt<@mut Context>)| {
|
2013-05-27 22:21:29 -05:00
|
|
|
match e.node {
|
2013-07-02 14:47:32 -05:00
|
|
|
ast::expr_binary(_, ref binop, l, r) => {
|
2013-05-27 22:21:29 -05:00
|
|
|
if is_comparison(*binop)
|
|
|
|
&& !check_limits(cx, *binop, l, r) {
|
|
|
|
cx.span_lint(type_limits, e.span,
|
|
|
|
"comparison is useless due to type limits");
|
|
|
|
}
|
2012-10-24 11:21:57 -05:00
|
|
|
}
|
2013-05-27 22:21:29 -05:00
|
|
|
_ => ()
|
2012-10-24 11:21:57 -05:00
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_expr(e, (cx, vt));
|
2013-05-27 22:21:29 -05:00
|
|
|
},
|
2013-01-08 08:21:19 -06:00
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
.. *oldvisit::default_visitor()
|
2013-05-07 01:07:00 -05:00
|
|
|
})
|
2012-04-19 20:04:41 -05:00
|
|
|
}
|
|
|
|
|
2013-06-27 08:04:22 -05:00
|
|
|
fn check_item_ctypes(cx: &Context, it: &ast::item) {
|
|
|
|
fn check_ty(cx: &Context, ty: &ast::Ty) {
|
2013-06-22 00:46:27 -05:00
|
|
|
match ty.node {
|
|
|
|
ast::ty_path(_, _, id) => {
|
|
|
|
match cx.tcx.def_map.get_copy(&id) {
|
|
|
|
ast::def_prim_ty(ast::ty_int(ast::ty_i)) => {
|
|
|
|
cx.span_lint(ctypes, ty.span,
|
|
|
|
"found rust type `int` in foreign module, while \
|
|
|
|
libc::c_int or libc::c_long should be used");
|
|
|
|
}
|
|
|
|
ast::def_prim_ty(ast::ty_uint(ast::ty_u)) => {
|
|
|
|
cx.span_lint(ctypes, ty.span,
|
|
|
|
"found rust type `uint` in foreign module, while \
|
|
|
|
libc::c_uint or libc::c_ulong should be used");
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 21:40:53 -05:00
|
|
|
ast::ty_ptr(ref mt) => { check_ty(cx, mt.ty) }
|
2013-06-22 00:46:27 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2013-04-30 00:15:17 -05:00
|
|
|
|
2013-05-27 22:21:29 -05:00
|
|
|
fn check_foreign_fn(cx: &Context, decl: &ast::fn_decl) {
|
2013-08-03 11:45:23 -05:00
|
|
|
for input in decl.inputs.iter() {
|
2013-07-31 16:59:59 -05:00
|
|
|
check_ty(cx, &input.ty);
|
2012-01-19 02:50:51 -06:00
|
|
|
}
|
2013-07-05 23:57:11 -05:00
|
|
|
check_ty(cx, &decl.output)
|
2012-01-19 02:50:51 -06:00
|
|
|
}
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match it.node {
|
2013-03-13 21:25:28 -05:00
|
|
|
ast::item_foreign_mod(ref nmod) if !nmod.abis.is_intrinsic() => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for ni in nmod.items.iter() {
|
2013-03-08 19:44:37 -06:00
|
|
|
match ni.node {
|
2013-08-02 16:30:00 -05:00
|
|
|
ast::foreign_item_fn(ref decl, _) => {
|
2013-06-22 00:46:27 -05:00
|
|
|
check_foreign_fn(cx, decl);
|
|
|
|
}
|
2013-07-05 23:57:11 -05:00
|
|
|
ast::foreign_item_static(ref t, _) => { check_ty(cx, t); }
|
2012-01-19 02:50:51 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-12 19:30:52 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {/* nothing to do */ }
|
2012-01-19 02:50:51 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2013-05-27 22:21:29 -05:00
|
|
|
fn check_type_for_lint(cx: &Context, lint: lint, span: span, ty: ty::t) {
|
2013-05-07 01:07:00 -05:00
|
|
|
if cx.get_level(lint) == allow { return }
|
2012-08-28 20:25:41 -05:00
|
|
|
|
2013-05-07 01:07:00 -05:00
|
|
|
let mut n_box = 0;
|
|
|
|
let mut n_uniq = 0;
|
|
|
|
ty::fold_ty(cx.tcx, ty, |t| {
|
|
|
|
match ty::get(t).sty {
|
|
|
|
ty::ty_box(_) => n_box += 1,
|
|
|
|
ty::ty_uniq(_) => n_uniq += 1,
|
|
|
|
_ => ()
|
|
|
|
};
|
|
|
|
t
|
|
|
|
});
|
2013-04-30 00:15:17 -05:00
|
|
|
|
2013-05-07 01:07:00 -05:00
|
|
|
if n_uniq > 0 && lint != managed_heap_memory {
|
|
|
|
let s = ty_to_str(cx.tcx, ty);
|
|
|
|
let m = ~"type uses owned (~ type) pointers: " + s;
|
|
|
|
cx.span_lint(lint, span, m);
|
|
|
|
}
|
2012-08-28 20:25:41 -05:00
|
|
|
|
2013-05-07 01:07:00 -05:00
|
|
|
if n_box > 0 && lint != owned_heap_memory {
|
|
|
|
let s = ty_to_str(cx.tcx, ty);
|
|
|
|
let m = ~"type uses managed (@ type) pointers: " + s;
|
|
|
|
cx.span_lint(lint, span, m);
|
2012-08-28 20:25:41 -05:00
|
|
|
}
|
2013-05-07 01:07:00 -05:00
|
|
|
}
|
2012-08-28 20:25:41 -05:00
|
|
|
|
2013-05-27 22:21:29 -05:00
|
|
|
fn check_type(cx: &Context, span: span, ty: ty::t) {
|
2013-06-21 07:29:53 -05:00
|
|
|
let xs = [managed_heap_memory, owned_heap_memory, heap_memory];
|
2013-08-03 11:45:23 -05:00
|
|
|
for lint in xs.iter() {
|
2013-05-07 01:07:00 -05:00
|
|
|
check_type_for_lint(cx, *lint, span, ty);
|
2012-08-28 20:25:41 -05:00
|
|
|
}
|
2013-05-07 01:07:00 -05:00
|
|
|
}
|
2012-08-28 20:25:41 -05:00
|
|
|
|
2013-06-27 08:04:22 -05:00
|
|
|
fn check_item_heap(cx: &Context, it: &ast::item) {
|
2013-05-07 01:07:00 -05:00
|
|
|
match it.node {
|
|
|
|
ast::item_fn(*) |
|
|
|
|
ast::item_ty(*) |
|
|
|
|
ast::item_enum(*) |
|
|
|
|
ast::item_struct(*) => check_type(cx, it.span,
|
|
|
|
ty::node_id_to_type(cx.tcx,
|
|
|
|
it.id)),
|
|
|
|
_ => ()
|
|
|
|
}
|
2013-04-30 00:15:17 -05:00
|
|
|
|
2013-05-07 01:07:00 -05:00
|
|
|
// If it's a struct, we also have to check the fields' types
|
|
|
|
match it.node {
|
|
|
|
ast::item_struct(struct_def, _) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for struct_field in struct_def.fields.iter() {
|
2013-05-07 01:07:00 -05:00
|
|
|
check_type(cx, struct_field.span,
|
|
|
|
ty::node_id_to_type(cx.tcx,
|
|
|
|
struct_field.node.id));
|
2013-01-28 20:34:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
2013-05-07 01:07:00 -05:00
|
|
|
}
|
2013-01-28 20:34:32 -06:00
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
fn lint_heap() -> oldvisit::vt<@mut Context> {
|
|
|
|
oldvisit::mk_vt(@oldvisit::Visitor {
|
|
|
|
visit_expr: |e,
|
|
|
|
(cx, vt): (@mut Context, oldvisit::vt<@mut Context>)| {
|
2013-04-30 00:15:17 -05:00
|
|
|
let ty = ty::expr_ty(cx.tcx, e);
|
|
|
|
check_type(cx, e.span, ty);
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_expr(e, (cx, vt));
|
2013-04-30 00:15:17 -05:00
|
|
|
},
|
2013-07-19 20:42:11 -05:00
|
|
|
.. *oldvisit::default_visitor()
|
2013-05-07 01:07:00 -05:00
|
|
|
})
|
2012-08-28 20:25:41 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
fn lint_path_statement() -> oldvisit::vt<@mut Context> {
|
|
|
|
oldvisit::mk_vt(@oldvisit::Visitor {
|
|
|
|
visit_stmt: |s,
|
|
|
|
(cx, vt): (@mut Context, oldvisit::vt<@mut Context>)| {
|
2013-04-30 00:15:17 -05:00
|
|
|
match s.node {
|
|
|
|
ast::stmt_semi(
|
|
|
|
@ast::expr { node: ast::expr_path(_), _ },
|
|
|
|
_
|
|
|
|
) => {
|
|
|
|
cx.span_lint(path_statement, s.span,
|
|
|
|
"path statement with no effect");
|
2013-01-08 16:00:45 -06:00
|
|
|
}
|
2013-04-30 00:15:17 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_stmt(s, (cx, vt));
|
2013-04-30 00:15:17 -05:00
|
|
|
},
|
2013-07-19 20:42:11 -05:00
|
|
|
.. *oldvisit::default_visitor()
|
2013-05-07 01:07:00 -05:00
|
|
|
})
|
2012-04-26 15:47:13 -05:00
|
|
|
}
|
|
|
|
|
2013-06-27 08:04:22 -05:00
|
|
|
fn check_item_non_camel_case_types(cx: &Context, it: &ast::item) {
|
2012-07-18 18:18:02 -05:00
|
|
|
fn is_camel_case(cx: ty::ctxt, ident: ast::ident) -> bool {
|
|
|
|
let ident = cx.sess.str_of(ident);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!ident.is_empty());
|
2013-06-11 10:32:49 -05:00
|
|
|
let ident = ident.trim_chars(&'_');
|
2013-06-30 22:50:39 -05:00
|
|
|
|
|
|
|
// start with a non-lowercase letter rather than non-uppercase
|
|
|
|
// ones (some scripts don't have a concept of upper/lowercase)
|
|
|
|
!ident.char_at(0).is_lowercase() &&
|
2012-07-31 20:58:03 -05:00
|
|
|
!ident.contains_char('_')
|
|
|
|
}
|
|
|
|
|
2013-07-25 14:26:38 -05:00
|
|
|
fn check_case(cx: &Context, sort: &str, ident: ast::ident, span: span) {
|
2013-04-30 00:15:17 -05:00
|
|
|
if !is_camel_case(cx.tcx, ident) {
|
2013-07-25 14:26:38 -05:00
|
|
|
cx.span_lint(
|
|
|
|
non_camel_case_types, span,
|
|
|
|
fmt!("%s `%s` should have a camel case identifier",
|
|
|
|
sort, cx.tcx.sess.str_of(ident)));
|
2012-07-31 20:58:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match it.node {
|
2013-07-25 14:26:38 -05:00
|
|
|
ast::item_ty(*) | ast::item_struct(*) => {
|
|
|
|
check_case(cx, "type", it.ident, it.span)
|
|
|
|
}
|
2013-02-16 12:36:09 -06:00
|
|
|
ast::item_trait(*) => {
|
2013-07-25 14:26:38 -05:00
|
|
|
check_case(cx, "trait", it.ident, it.span)
|
2012-07-31 20:58:03 -05:00
|
|
|
}
|
2013-02-16 12:36:09 -06:00
|
|
|
ast::item_enum(ref enum_definition, _) => {
|
2013-07-25 14:26:38 -05:00
|
|
|
check_case(cx, "type", it.ident, it.span);
|
2013-08-03 11:45:23 -05:00
|
|
|
for variant in enum_definition.variants.iter() {
|
2013-07-25 14:26:38 -05:00
|
|
|
check_case(cx, "variant", variant.node.name, variant.span);
|
2013-02-16 12:36:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
2012-07-31 20:58:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 22:51:13 -05:00
|
|
|
fn check_item_non_uppercase_statics(cx: &Context, it: &ast::item) {
|
|
|
|
match it.node {
|
|
|
|
// only check static constants
|
|
|
|
ast::item_static(_, ast::m_imm, _) => {
|
|
|
|
let s = cx.tcx.sess.str_of(it.ident);
|
|
|
|
// check for lowercase letters rather than non-uppercase
|
|
|
|
// ones (some scripts don't have a concept of
|
|
|
|
// upper/lowercase)
|
2013-07-04 21:13:26 -05:00
|
|
|
if s.iter().any(|c| c.is_lowercase()) {
|
2013-06-30 22:51:13 -05:00
|
|
|
cx.span_lint(non_uppercase_statics, it.span,
|
|
|
|
"static constant should have an uppercase identifier");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
fn lint_unused_unsafe() -> oldvisit::vt<@mut Context> {
|
|
|
|
oldvisit::mk_vt(@oldvisit::Visitor {
|
|
|
|
visit_expr: |e,
|
|
|
|
(cx, vt): (@mut Context, oldvisit::vt<@mut Context>)| {
|
2013-05-27 22:21:29 -05:00
|
|
|
match e.node {
|
2013-07-27 03:25:59 -05:00
|
|
|
ast::expr_block(ref blk) if blk.rules == ast::UnsafeBlock => {
|
2013-07-16 13:08:35 -05:00
|
|
|
if !cx.tcx.used_unsafe.contains(&blk.id) {
|
2013-05-27 22:21:29 -05:00
|
|
|
cx.span_lint(unused_unsafe, blk.span,
|
|
|
|
"unnecessary `unsafe` block");
|
|
|
|
}
|
2013-04-09 00:31:10 -05:00
|
|
|
}
|
2013-05-27 22:21:29 -05:00
|
|
|
_ => ()
|
2013-04-09 00:31:10 -05:00
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_expr(e, (cx, vt));
|
2013-05-27 22:21:29 -05:00
|
|
|
},
|
2013-07-19 20:42:11 -05:00
|
|
|
.. *oldvisit::default_visitor()
|
2013-05-07 01:07:00 -05:00
|
|
|
})
|
2013-04-09 00:31:10 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
fn lint_unused_mut() -> oldvisit::vt<@mut Context> {
|
2013-05-27 22:21:29 -05:00
|
|
|
fn check_pat(cx: &Context, p: @ast::pat) {
|
2013-04-12 00:09:54 -05:00
|
|
|
let mut used = false;
|
|
|
|
let mut bindings = 0;
|
2013-04-30 00:15:17 -05:00
|
|
|
do pat_util::pat_bindings(cx.tcx.def_map, p) |_, id, _, _| {
|
|
|
|
used = used || cx.tcx.used_mut_nodes.contains(&id);
|
2013-04-12 00:09:54 -05:00
|
|
|
bindings += 1;
|
|
|
|
}
|
|
|
|
if !used {
|
|
|
|
let msg = if bindings == 1 {
|
2013-04-30 11:47:52 -05:00
|
|
|
"variable does not need to be mutable"
|
2013-04-12 00:09:54 -05:00
|
|
|
} else {
|
2013-04-30 11:47:52 -05:00
|
|
|
"variables do not need to be mutable"
|
2013-04-12 00:09:54 -05:00
|
|
|
};
|
2013-04-30 00:15:17 -05:00
|
|
|
cx.span_lint(unused_mut, p.span, msg);
|
2013-04-12 00:09:54 -05:00
|
|
|
}
|
2013-05-27 22:21:29 -05:00
|
|
|
}
|
2013-04-12 00:09:54 -05:00
|
|
|
|
2013-05-27 22:21:29 -05:00
|
|
|
fn visit_fn_decl(cx: &Context, fd: &ast::fn_decl) {
|
2013-08-03 11:45:23 -05:00
|
|
|
for arg in fd.inputs.iter() {
|
2013-04-12 00:09:54 -05:00
|
|
|
if arg.is_mutbl {
|
2013-05-27 22:21:29 -05:00
|
|
|
check_pat(cx, arg.pat);
|
2013-04-12 00:09:54 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-27 22:21:29 -05:00
|
|
|
}
|
2013-04-12 00:09:54 -05:00
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::mk_vt(@oldvisit::Visitor {
|
|
|
|
visit_local: |l,
|
|
|
|
(cx, vt): (@mut Context, oldvisit::vt<@mut Context>)| {
|
2013-07-19 00:38:55 -05:00
|
|
|
if l.is_mutbl {
|
|
|
|
check_pat(cx, l.pat);
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_local(l, (cx, vt));
|
2013-05-27 22:21:29 -05:00
|
|
|
},
|
2013-06-11 21:55:16 -05:00
|
|
|
visit_fn: |a, fd, b, c, d, (cx, vt)| {
|
2013-05-27 22:21:29 -05:00
|
|
|
visit_fn_decl(cx, fd);
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_fn(a, fd, b, c, d, (cx, vt));
|
2013-05-27 22:21:29 -05:00
|
|
|
},
|
2013-06-11 21:55:16 -05:00
|
|
|
visit_ty_method: |tm, (cx, vt)| {
|
2013-05-27 22:21:29 -05:00
|
|
|
visit_fn_decl(cx, &tm.decl);
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_ty_method(tm, (cx, vt));
|
2013-04-30 00:15:17 -05:00
|
|
|
},
|
2013-06-11 21:55:16 -05:00
|
|
|
visit_trait_method: |tm, (cx, vt)| {
|
2013-04-30 00:15:17 -05:00
|
|
|
match *tm {
|
2013-05-27 22:21:29 -05:00
|
|
|
ast::required(ref tm) => visit_fn_decl(cx, &tm.decl),
|
|
|
|
ast::provided(m) => visit_fn_decl(cx, &m.decl)
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_trait_method(tm, (cx, vt));
|
2013-04-30 00:15:17 -05:00
|
|
|
},
|
2013-07-19 20:42:11 -05:00
|
|
|
.. *oldvisit::default_visitor()
|
2013-05-07 01:07:00 -05:00
|
|
|
})
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
fn lint_session(cx: @mut Context) -> @visit::Visitor<()> {
|
|
|
|
ast_util::id_visitor(|id| {
|
2013-04-30 00:15:17 -05:00
|
|
|
match cx.tcx.sess.lints.pop(&id) {
|
|
|
|
None => {},
|
|
|
|
Some(l) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for (lint, span, msg) in l.consume_iter() {
|
2013-04-30 00:15:17 -05:00
|
|
|
cx.span_lint(lint, span, msg)
|
2013-04-12 00:09:54 -05:00
|
|
|
}
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
}, false)
|
2012-07-25 08:41:56 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
fn lint_unnecessary_allocations() -> oldvisit::vt<@mut Context> {
|
2013-05-23 09:58:23 -05:00
|
|
|
// Warn if string and vector literals with sigils are immediately borrowed.
|
|
|
|
// Those can have the sigil removed.
|
2013-06-27 08:04:22 -05:00
|
|
|
fn check(cx: &Context, e: &ast::expr) {
|
2013-05-18 02:07:28 -05:00
|
|
|
match e.node {
|
|
|
|
ast::expr_vstore(e2, ast::expr_vstore_uniq) |
|
|
|
|
ast::expr_vstore(e2, ast::expr_vstore_box) => {
|
|
|
|
match e2.node {
|
|
|
|
ast::expr_lit(@codemap::spanned{
|
|
|
|
node: ast::lit_str(*), _}) |
|
|
|
|
ast::expr_vec(*) => {}
|
|
|
|
_ => return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => return
|
|
|
|
}
|
|
|
|
|
2013-05-23 09:58:23 -05:00
|
|
|
match cx.tcx.adjustments.find_copy(&e.id) {
|
|
|
|
Some(@ty::AutoDerefRef(ty::AutoDerefRef {
|
|
|
|
autoref: Some(ty::AutoBorrowVec(*)), _ })) => {
|
2013-05-18 02:07:28 -05:00
|
|
|
cx.span_lint(unnecessary_allocation,
|
|
|
|
e.span, "unnecessary allocation, the sigil can be \
|
|
|
|
removed");
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::mk_vt(@oldvisit::Visitor {
|
|
|
|
visit_expr: |e,
|
|
|
|
(cx, vt): (@mut Context, oldvisit::vt<@mut Context>)| {
|
2013-05-27 22:21:29 -05:00
|
|
|
check(cx, e);
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_expr(e, (cx, vt));
|
2013-05-27 22:21:29 -05:00
|
|
|
},
|
2013-07-19 20:42:11 -05:00
|
|
|
.. *oldvisit::default_visitor()
|
2013-05-18 02:07:28 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
fn lint_missing_doc() -> oldvisit::vt<@mut Context> {
|
|
|
|
fn check_attrs(cx: @mut Context,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
sp: span,
|
|
|
|
msg: &str) {
|
2013-05-28 16:53:43 -05:00
|
|
|
// If we're building a test harness, then warning about documentation is
|
|
|
|
// probably not really relevant right now
|
|
|
|
if cx.tcx.sess.opts.test { return }
|
|
|
|
// If we have doc(hidden), nothing to do
|
|
|
|
if cx.doc_hidden { return }
|
|
|
|
// If we're documented, nothing to do
|
2013-07-04 21:13:26 -05:00
|
|
|
if attrs.iter().any(|a| a.node.is_sugared_doc) { return }
|
2013-05-28 16:53:43 -05:00
|
|
|
|
|
|
|
// otherwise, warn!
|
|
|
|
cx.span_lint(missing_doc, sp, msg);
|
2013-05-28 15:44:53 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::mk_vt(@oldvisit::Visitor {
|
2013-06-11 21:55:16 -05:00
|
|
|
visit_ty_method: |m, (cx, vt)| {
|
2013-05-28 15:44:53 -05:00
|
|
|
// All ty_method objects are linted about because they're part of a
|
|
|
|
// trait (no visibility)
|
|
|
|
check_attrs(cx, m.attrs, m.span,
|
|
|
|
"missing documentation for a method");
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_ty_method(m, (cx, vt));
|
2013-05-28 15:44:53 -05:00
|
|
|
},
|
2013-05-24 07:13:41 -05:00
|
|
|
|
2013-06-11 21:55:16 -05:00
|
|
|
visit_fn: |fk, d, b, sp, id, (cx, vt)| {
|
2013-05-28 15:44:53 -05:00
|
|
|
// Only warn about explicitly public methods. Soon implicit
|
|
|
|
// public-ness will hopefully be going away.
|
|
|
|
match *fk {
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::fk_method(_, _, m) if m.vis == ast::public => {
|
2013-05-28 15:44:53 -05:00
|
|
|
// If we're in a trait implementation, no need to duplicate
|
|
|
|
// documentation
|
|
|
|
if !cx.in_trait_impl {
|
|
|
|
check_attrs(cx, m.attrs, sp,
|
|
|
|
"missing documentation for a method");
|
2013-05-24 07:13:41 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-27 22:21:29 -05:00
|
|
|
|
2013-05-28 15:44:53 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_fn(fk, d, b, sp, id, (cx, vt));
|
2013-05-24 03:27:31 -05:00
|
|
|
},
|
|
|
|
|
2013-06-11 21:55:16 -05:00
|
|
|
visit_item: |it, (cx, vt)| {
|
2013-05-28 15:44:53 -05:00
|
|
|
match it.node {
|
|
|
|
// Go ahead and match the fields here instead of using
|
|
|
|
// visit_struct_field while we have access to the enclosing
|
|
|
|
// struct's visibility
|
|
|
|
ast::item_struct(sdef, _) if it.vis == ast::public => {
|
|
|
|
check_attrs(cx, it.attrs, it.span,
|
|
|
|
"missing documentation for a struct");
|
2013-08-03 11:45:23 -05:00
|
|
|
for field in sdef.fields.iter() {
|
2013-05-28 15:44:53 -05:00
|
|
|
match field.node.kind {
|
|
|
|
ast::named_field(_, vis) if vis != ast::private => {
|
|
|
|
check_attrs(cx, field.node.attrs, field.span,
|
|
|
|
"missing documentation for a field");
|
2013-05-24 07:13:41 -05:00
|
|
|
}
|
2013-05-28 15:44:53 -05:00
|
|
|
ast::unnamed_field | ast::named_field(*) => {}
|
2013-05-24 03:27:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-28 15:44:53 -05:00
|
|
|
|
|
|
|
ast::item_trait(*) if it.vis == ast::public => {
|
|
|
|
check_attrs(cx, it.attrs, it.span,
|
|
|
|
"missing documentation for a trait");
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::item_fn(*) if it.vis == ast::public => {
|
|
|
|
check_attrs(cx, it.attrs, it.span,
|
|
|
|
"missing documentation for a function");
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
2013-05-24 03:27:31 -05:00
|
|
|
};
|
2013-05-28 15:44:53 -05:00
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_item(it, (cx, vt));
|
2013-05-24 03:27:31 -05:00
|
|
|
},
|
2013-05-28 15:44:53 -05:00
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
.. *oldvisit::default_visitor()
|
2013-05-24 03:27:31 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-07-19 00:38:55 -05:00
|
|
|
pub fn check_crate(tcx: ty::ctxt, crate: @ast::Crate) {
|
2013-04-30 00:15:17 -05:00
|
|
|
let cx = @mut Context {
|
|
|
|
dict: @get_lint_dict(),
|
|
|
|
curr: SmallIntMap::new(),
|
|
|
|
tcx: tcx,
|
|
|
|
lint_stack: ~[],
|
2013-05-07 01:07:00 -05:00
|
|
|
visitors: ~[],
|
2013-05-28 15:44:53 -05:00
|
|
|
in_trait_impl: false,
|
2013-05-28 16:53:43 -05:00
|
|
|
doc_hidden: false,
|
2013-04-30 00:15:17 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Install defaults.
|
2013-08-03 11:45:23 -05:00
|
|
|
for (_, spec) in cx.dict.iter() {
|
2013-05-09 12:22:26 -05:00
|
|
|
cx.set_level(spec.lint, spec.default, Default);
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Install command-line options, overriding defaults.
|
2013-08-03 11:45:23 -05:00
|
|
|
for &(lint, level) in tcx.sess.opts.lint_opts.iter() {
|
2013-05-09 12:22:26 -05:00
|
|
|
cx.set_level(lint, level, CommandLine);
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
|
2013-05-07 01:07:00 -05:00
|
|
|
// Register each of the lint passes with the context
|
2013-07-19 20:42:11 -05:00
|
|
|
cx.add_oldvisit_lint(lint_while_true());
|
|
|
|
cx.add_oldvisit_lint(lint_path_statement());
|
|
|
|
cx.add_oldvisit_lint(lint_heap());
|
|
|
|
cx.add_oldvisit_lint(lint_type_limits());
|
|
|
|
cx.add_oldvisit_lint(lint_unused_unsafe());
|
|
|
|
cx.add_oldvisit_lint(lint_unused_mut());
|
|
|
|
cx.add_oldvisit_lint(lint_unnecessary_allocations());
|
|
|
|
cx.add_oldvisit_lint(lint_missing_doc());
|
|
|
|
cx.add_lint(lint_session(cx));
|
2012-04-12 19:30:52 -05:00
|
|
|
|
2013-05-07 01:07:00 -05:00
|
|
|
// Actually perform the lint checks (iterating the ast)
|
2013-07-19 00:38:55 -05:00
|
|
|
do cx.with_lint_attrs(crate.attrs) {
|
2013-05-07 01:07:00 -05:00
|
|
|
cx.process(Crate(crate));
|
2013-04-30 00:15:17 -05:00
|
|
|
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_crate(crate, (cx, oldvisit::mk_vt(@oldvisit::Visitor {
|
|
|
|
visit_item: |it,
|
|
|
|
(cx, vt):
|
|
|
|
(@mut Context, oldvisit::vt<@mut Context>)| {
|
2013-05-27 22:21:29 -05:00
|
|
|
do cx.with_lint_attrs(it.attrs) {
|
2013-05-28 15:44:53 -05:00
|
|
|
match it.node {
|
|
|
|
ast::item_impl(_, Some(*), _, _) => {
|
|
|
|
cx.in_trait_impl = true;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2013-05-27 22:21:29 -05:00
|
|
|
check_item_ctypes(cx, it);
|
|
|
|
check_item_non_camel_case_types(cx, it);
|
2013-06-30 22:51:13 -05:00
|
|
|
check_item_non_uppercase_statics(cx, it);
|
2013-05-27 22:21:29 -05:00
|
|
|
check_item_heap(cx, it);
|
|
|
|
|
|
|
|
cx.process(Item(it));
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_item(it, (cx, vt));
|
2013-05-28 15:44:53 -05:00
|
|
|
cx.in_trait_impl = false;
|
2013-05-27 22:21:29 -05:00
|
|
|
}
|
|
|
|
},
|
2013-06-11 21:55:16 -05:00
|
|
|
visit_fn: |fk, decl, body, span, id, (cx, vt)| {
|
2013-04-30 00:15:17 -05:00
|
|
|
match *fk {
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::fk_method(_, _, m) => {
|
2013-04-30 00:15:17 -05:00
|
|
|
do cx.with_lint_attrs(m.attrs) {
|
2013-05-07 01:07:00 -05:00
|
|
|
cx.process(Method(m));
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_fn(fk,
|
|
|
|
decl,
|
|
|
|
body,
|
|
|
|
span,
|
|
|
|
id,
|
|
|
|
(cx, vt));
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_fn(fk,
|
|
|
|
decl,
|
|
|
|
body,
|
|
|
|
span,
|
|
|
|
id,
|
|
|
|
(cx, vt));
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-07-19 20:42:11 -05:00
|
|
|
.. *oldvisit::default_visitor()
|
2013-06-11 21:55:16 -05:00
|
|
|
})));
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
|
2013-05-07 01:07:00 -05:00
|
|
|
// If we missed any lints added to the session, then there's a bug somewhere
|
|
|
|
// in the iteration code.
|
2013-08-03 11:45:23 -05:00
|
|
|
for (id, v) in tcx.sess.lints.iter() {
|
|
|
|
for t in v.iter() {
|
2013-04-30 00:15:17 -05:00
|
|
|
match *t {
|
|
|
|
(lint, span, ref msg) =>
|
2013-07-19 20:42:11 -05:00
|
|
|
tcx.sess.span_bug(span, fmt!("unprocessed lint %? at %s: \
|
|
|
|
%s",
|
|
|
|
lint,
|
|
|
|
ast_map::node_id_to_str(
|
|
|
|
tcx.items,
|
|
|
|
*id,
|
|
|
|
token::get_ident_interner()),
|
|
|
|
*msg))
|
2013-04-30 00:15:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 01:07:00 -05:00
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
tcx.sess.abort_if_errors();
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|