2013-10-08 20:26:09 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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-10-02 13:29:29 -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.
|
|
|
|
//!
|
|
|
|
//! 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
|
|
|
|
//! level at that location is.
|
|
|
|
//!
|
|
|
|
//! 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.
|
|
|
|
//!
|
|
|
|
//! 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 few linting functions and
|
|
|
|
//! modify the Context visitor appropriately. If you're adding lints from the
|
|
|
|
//! Context itself, span_lint should be used instead of add_lint.
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use driver::session;
|
2013-11-11 22:17:47 -06:00
|
|
|
use middle::privacy;
|
2013-06-02 15:03:35 -05:00
|
|
|
use middle::trans::adt; // for `adt::is_ffi_safe`
|
2012-09-04 13:54:36 -05:00
|
|
|
use middle::ty;
|
2013-12-15 20:52:48 -06:00
|
|
|
use middle::typeck;
|
2013-04-12 00:09:54 -05:00
|
|
|
use middle::pat_util;
|
2013-08-31 02:13:57 -05:00
|
|
|
use metadata::csearch;
|
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-08-31 02:13:57 -05:00
|
|
|
use syntax::attr::{AttrMetaMethods, AttributeMethods};
|
2013-08-31 11:13:04 -05: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;
|
2013-08-14 03:24:42 -05:00
|
|
|
use syntax::{ast, ast_util, visit};
|
2013-11-26 16:55:06 -06:00
|
|
|
use syntax::ast_util::IdVisitingOperation;
|
2013-08-14 03:24:42 -05:00
|
|
|
use syntax::visit::Visitor;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-12-19 21:42:00 -06:00
|
|
|
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
|
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,
|
2013-09-30 10:44:58 -05:00
|
|
|
non_uppercase_pattern_statics,
|
2012-10-24 11:21:57 -05:00
|
|
|
type_limits,
|
2013-10-27 13:59:58 -05:00
|
|
|
type_overflow,
|
2013-04-09 00:31:10 -05:00
|
|
|
unused_unsafe,
|
2013-11-21 16:36:54 -06:00
|
|
|
unsafe_block,
|
2013-11-04 09:35:56 -06:00
|
|
|
attribute_usage,
|
2013-11-26 16:55:06 -06:00
|
|
|
unknown_features,
|
2014-01-01 15:26:07 -06:00
|
|
|
unknown_crate_type,
|
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-12-08 01:55:27 -06:00
|
|
|
dead_code,
|
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
|
|
|
|
2013-08-31 02:13:57 -05:00
|
|
|
deprecated,
|
|
|
|
experimental,
|
|
|
|
unstable,
|
|
|
|
|
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-12-19 21:42:00 -06:00
|
|
|
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
|
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-12-19 21:42:00 -06:00
|
|
|
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
|
2013-07-16 23:12:16 -05:00
|
|
|
pub struct LintSpec {
|
2013-12-19 21:42:00 -06:00
|
|
|
default: level,
|
2013-02-19 01:40:42 -06:00
|
|
|
lint: lint,
|
2013-03-14 13:22:51 -05:00
|
|
|
desc: &'static str,
|
2013-07-16 23:12:16 -05:00
|
|
|
}
|
|
|
|
|
2013-06-12 12:02:55 -05:00
|
|
|
pub type LintDict = HashMap<&'static str, LintSpec>;
|
2013-04-30 00:15:17 -05:00
|
|
|
|
2013-05-09 12:22:26 -05:00
|
|
|
#[deriving(Eq)]
|
|
|
|
enum LintSource {
|
2013-08-31 11:13:04 -05:00
|
|
|
Node(Span),
|
2013-05-09 12:22:26 -05:00
|
|
|
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-09-30 10:44:58 -05:00
|
|
|
("non_uppercase_pattern_statics",
|
|
|
|
LintSpec {
|
|
|
|
lint: non_uppercase_pattern_statics,
|
2013-09-30 20:10:19 -05:00
|
|
|
desc: "static constants in match patterns should be all caps",
|
2013-09-30 10:44:58 -05:00
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
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
|
|
|
|
}),
|
|
|
|
|
2013-10-27 13:59:58 -05:00
|
|
|
("type_overflow",
|
|
|
|
LintSpec {
|
|
|
|
lint: type_overflow,
|
|
|
|
desc: "literal out of range for its type",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
2013-05-07 02:28:58 -05:00
|
|
|
("unused_unsafe",
|
|
|
|
LintSpec {
|
|
|
|
lint: unused_unsafe,
|
|
|
|
desc: "unnecessary use of an `unsafe` block",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
2013-11-21 16:36:54 -06:00
|
|
|
("unsafe_block",
|
|
|
|
LintSpec {
|
|
|
|
lint: unsafe_block,
|
|
|
|
desc: "usage of an `unsafe` block",
|
|
|
|
default: allow
|
|
|
|
}),
|
|
|
|
|
2013-11-04 09:35:56 -06:00
|
|
|
("attribute_usage",
|
|
|
|
LintSpec {
|
|
|
|
lint: attribute_usage,
|
|
|
|
desc: "detects bad use of attributes",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
2013-05-07 02:28:58 -05:00
|
|
|
("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-12-08 01:55:27 -06:00
|
|
|
("dead_code",
|
|
|
|
LintSpec {
|
|
|
|
lint: dead_code,
|
|
|
|
desc: "detect piece of code that will never be used",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
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
|
|
|
|
2013-08-31 02:13:57 -05:00
|
|
|
("deprecated",
|
|
|
|
LintSpec {
|
|
|
|
lint: deprecated,
|
|
|
|
desc: "detects use of #[deprecated] items",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
("experimental",
|
|
|
|
LintSpec {
|
|
|
|
lint: experimental,
|
|
|
|
desc: "detects use of #[experimental] items",
|
|
|
|
default: warn
|
|
|
|
}),
|
|
|
|
|
|
|
|
("unstable",
|
|
|
|
LintSpec {
|
|
|
|
lint: unstable,
|
|
|
|
desc: "detects use of #[unstable] items (incl. items with no stability attribute)",
|
|
|
|
default: allow
|
|
|
|
}),
|
|
|
|
|
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-11-26 16:55:06 -06:00
|
|
|
|
|
|
|
("unknown_features",
|
|
|
|
LintSpec {
|
|
|
|
lint: unknown_features,
|
2014-01-01 22:50:11 -06:00
|
|
|
desc: "unknown features found in crate-level #[feature] directives",
|
2013-11-26 16:55:06 -06:00
|
|
|
default: deny,
|
|
|
|
}),
|
2014-01-01 15:26:07 -06:00
|
|
|
|
|
|
|
("unknown_crate_type",
|
|
|
|
LintSpec {
|
|
|
|
lint: unknown_crate_type,
|
|
|
|
desc: "unknown crate type found in #[crate_type] directive",
|
|
|
|
default: deny,
|
|
|
|
}),
|
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-12-10 01:16:18 -06:00
|
|
|
struct Context<'a> {
|
2013-04-30 00:15:17 -05:00
|
|
|
// All known lint modes (string versions)
|
|
|
|
dict: @LintDict,
|
|
|
|
// Current levels of each lint warning
|
2013-10-02 13:29:29 -05:00
|
|
|
cur: 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-12-15 20:52:48 -06:00
|
|
|
// maps from an expression id that corresponds to a method call to the
|
|
|
|
// details of the method to be invoked
|
|
|
|
method_map: typeck::method_map,
|
2013-11-11 22:17:47 -06:00
|
|
|
// Items exported by the crate; used by the missing_doc lint.
|
2013-12-10 01:16:18 -06:00
|
|
|
exported_items: &'a privacy::ExportedItems,
|
2013-11-11 22:17:47 -06:00
|
|
|
// The id of the current `ast::struct_def` being walked.
|
|
|
|
cur_struct_def_id: ast::NodeId,
|
|
|
|
// Whether some ancestor of the current node was marked
|
|
|
|
// #[doc(hidden)].
|
|
|
|
is_doc_hidden: bool,
|
2013-10-02 13:29:29 -05:00
|
|
|
|
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-10-27 13:59:58 -05:00
|
|
|
|
|
|
|
// id of the last visited negated expression
|
|
|
|
negated_expr_id: ast::NodeId
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Context<'a> {
|
2013-02-19 01:40:42 -06:00
|
|
|
fn get_level(&self, lint: lint) -> level {
|
2013-10-02 13:29:29 -05:00
|
|
|
match self.cur.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 {
|
2013-10-02 13:29:29 -05:00
|
|
|
match self.cur.find(&(lint as uint)) {
|
2013-05-09 12:22:26 -05:00
|
|
|
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-10-02 13:29:29 -05:00
|
|
|
self.cur.remove(&(lint as uint));
|
2012-04-12 19:30:52 -05:00
|
|
|
} else {
|
2013-10-02 13:29:29 -05:00
|
|
|
self.cur.insert(lint as uint, (level, src));
|
2013-05-09 12:22:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-21 15:08:31 -05:00
|
|
|
fail!("unregistered lint {:?}", lint);
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2013-08-31 11:13:04 -05:00
|
|
|
fn span_lint(&self, lint: lint, span: Span, msg: &str) {
|
2013-10-02 13:29:29 -05:00
|
|
|
let (level, src) = match self.cur.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 {
|
2013-10-08 20:26:09 -05:00
|
|
|
Default => {
|
|
|
|
format!("{}, \\#[{}({})] on by default", msg,
|
|
|
|
level_to_str(level), self.lint_to_str(lint))
|
|
|
|
},
|
|
|
|
CommandLine => {
|
|
|
|
format!("{} [-{} {}]", msg,
|
|
|
|
match level {
|
2013-05-09 12:22:26 -05:00
|
|
|
warn => 'W', deny => 'D', forbid => 'F',
|
2013-10-21 15:08:31 -05:00
|
|
|
allow => fail!()
|
2013-10-08 20:26:09 -05:00
|
|
|
}, self.lint_to_str(lint).replace("_", "-"))
|
2013-05-09 12:22:26 -05:00
|
|
|
},
|
|
|
|
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); }
|
2013-10-21 15:08:31 -05:00
|
|
|
allow => fail!(),
|
2013-05-09 12:22:26 -05:00
|
|
|
}
|
|
|
|
|
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-11-19 15:22:03 -06:00
|
|
|
fn with_lint_attrs(&mut self,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
f: |&mut Context|) {
|
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-11-21 17:42:55 -06:00
|
|
|
each_lint(self.tcx.sess, attrs, |meta, level, lintname| {
|
2013-08-02 01:17:20 -05:00
|
|
|
match self.dict.find_equiv(&lintname) {
|
|
|
|
None => {
|
|
|
|
self.span_lint(
|
|
|
|
unrecognized_lint,
|
|
|
|
meta.span,
|
2013-09-28 00:38:08 -05:00
|
|
|
format!("unknown `{}` attribute: `{}`",
|
2013-08-02 01:17:20 -05:00
|
|
|
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,
|
2013-09-28 00:38:08 -05:00
|
|
|
format!("{}({}) overruled by outer forbid({})",
|
2013-08-02 01:17:20 -05:00
|
|
|
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
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2013-11-11 22:17:47 -06:00
|
|
|
let old_is_doc_hidden = self.is_doc_hidden;
|
|
|
|
self.is_doc_hidden = self.is_doc_hidden ||
|
|
|
|
attrs.iter().any(|attr| ("doc" == attr.name() && match attr.meta_item_list()
|
|
|
|
{ None => false,
|
|
|
|
Some(l) => attr::contains_name(l, "hidden") }));
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
f(self);
|
2012-06-04 18:07:54 -05:00
|
|
|
|
2013-04-30 00:15:17 -05:00
|
|
|
// rollback
|
2013-11-11 22:17:47 -06:00
|
|
|
self.is_doc_hidden = old_is_doc_hidden;
|
2013-11-21 17:42:55 -06:00
|
|
|
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-11-21 17:42:55 -06:00
|
|
|
})
|
2012-06-04 18:07:54 -05:00
|
|
|
}
|
|
|
|
|
2013-11-19 15:22:03 -06:00
|
|
|
fn visit_ids(&self, f: |&mut ast_util::IdVisitor<Context>|) {
|
2013-10-02 13:29:29 -05:00
|
|
|
let mut v = ast_util::IdVisitor {
|
|
|
|
operation: self,
|
|
|
|
pass_through_items: false,
|
|
|
|
visited_outermost: false,
|
|
|
|
};
|
|
|
|
f(&mut v);
|
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],
|
2013-11-19 15:22:03 -06:00
|
|
|
f: |@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-10-01 16:31:03 -05:00
|
|
|
continue;
|
2013-05-07 01:07:00 -05:00
|
|
|
}
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn check_while_true_expr(cx: &Context, e: &ast::Expr) {
|
|
|
|
match e.node {
|
|
|
|
ast::ExprWhile(cond, _) => {
|
|
|
|
match cond.node {
|
|
|
|
ast::ExprLit(@codemap::Spanned {
|
2013-11-28 14:22:53 -06:00
|
|
|
node: ast::lit_bool(true), ..}) =>
|
2013-10-02 13:29:29 -05:00
|
|
|
{
|
|
|
|
cx.span_lint(while_true, e.span,
|
|
|
|
"denote infinite loops with loop { ... }");
|
2012-04-19 20:04:41 -05:00
|
|
|
}
|
2013-04-30 00:15:17 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
|
|
|
_ => ()
|
2013-08-14 03:24:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn check_type_limits(cx: &Context, e: &ast::Expr) {
|
|
|
|
return match e.node {
|
|
|
|
ast::ExprBinary(_, binop, l, r) => {
|
|
|
|
if is_comparison(binop) && !check_limits(cx.tcx, binop, l, r) {
|
|
|
|
cx.span_lint(type_limits, e.span,
|
|
|
|
"comparison is useless due to type limits");
|
2013-08-14 03:24:42 -05:00
|
|
|
}
|
2013-10-27 13:59:58 -05:00
|
|
|
},
|
|
|
|
ast::ExprLit(lit) => {
|
|
|
|
match ty::get(ty::expr_ty(cx.tcx, e)).sty {
|
|
|
|
ty::ty_int(t) => {
|
|
|
|
let int_type = if t == ast::ty_i {
|
|
|
|
cx.tcx.sess.targ_cfg.int_type
|
|
|
|
} else { t };
|
|
|
|
let (min, max) = int_ty_range(int_type);
|
|
|
|
let mut lit_val: i64 = match lit.node {
|
|
|
|
ast::lit_int(v, _) => v,
|
|
|
|
ast::lit_uint(v, _) => v as i64,
|
|
|
|
ast::lit_int_unsuffixed(v) => v,
|
|
|
|
_ => fail!()
|
|
|
|
};
|
|
|
|
if cx.negated_expr_id == e.id {
|
|
|
|
lit_val *= -1;
|
|
|
|
}
|
|
|
|
if lit_val < min || lit_val > max {
|
|
|
|
cx.span_lint(type_overflow, e.span,
|
|
|
|
"literal out of range for its type");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ty::ty_uint(t) => {
|
|
|
|
let uint_type = if t == ast::ty_u {
|
|
|
|
cx.tcx.sess.targ_cfg.uint_type
|
|
|
|
} else { t };
|
|
|
|
let (min, max) = uint_ty_range(uint_type);
|
|
|
|
let lit_val: u64 = match lit.node {
|
|
|
|
ast::lit_int(v, _) => v as u64,
|
|
|
|
ast::lit_uint(v, _) => v,
|
|
|
|
ast::lit_int_unsuffixed(v) => v as u64,
|
|
|
|
_ => fail!()
|
|
|
|
};
|
|
|
|
if lit_val < min || lit_val > max {
|
|
|
|
cx.span_lint(type_overflow, e.span,
|
|
|
|
"literal out of range for its type");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => ()
|
|
|
|
};
|
|
|
|
},
|
2013-10-02 13:29:29 -05:00
|
|
|
_ => ()
|
|
|
|
};
|
2013-08-14 03:24:42 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn is_valid<T:cmp::Ord>(binop: ast::BinOp, v: T,
|
|
|
|
min: T, max: T) -> bool {
|
2012-10-24 11:21:57 -05:00
|
|
|
match binop {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiLt => v <= max,
|
|
|
|
ast::BiLe => v < max,
|
|
|
|
ast::BiGt => v >= min,
|
|
|
|
ast::BiGe => v > min,
|
|
|
|
ast::BiEq | ast::BiNe => v >= min && v <= max,
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!()
|
2012-10-24 11:21:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn rev_binop(binop: ast::BinOp) -> ast::BinOp {
|
2012-10-24 11:21:57 -05:00
|
|
|
match binop {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiLt => ast::BiGt,
|
|
|
|
ast::BiLe => ast::BiGe,
|
|
|
|
ast::BiGt => ast::BiLt,
|
|
|
|
ast::BiGe => ast::BiLe,
|
2012-10-24 11:21:57 -05:00
|
|
|
_ => 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-10-02 13:29:29 -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_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-10-02 13:29:29 -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-10-02 13:29:29 -05:00
|
|
|
fn check_limits(tcx: ty::ctxt, 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) {
|
2013-09-01 20:45:37 -05:00
|
|
|
(&ast::ExprLit(_), _) => (l, r, true),
|
|
|
|
(_, &ast::ExprLit(_)) => (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-10-02 13:29:29 -05:00
|
|
|
let norm_binop = if swap { rev_binop(binop) } else { binop };
|
|
|
|
match ty::get(ty::expr_ty(tcx, expr)).sty {
|
2012-10-24 11:21:57 -05:00
|
|
|
ty::ty_int(int_ty) => {
|
2013-10-02 13:29:29 -05:00
|
|
|
let (min, max) = int_ty_range(int_ty);
|
2012-10-24 11:21:57 -05:00
|
|
|
let lit_val: i64 = match lit.node {
|
2013-10-02 13:29:29 -05:00
|
|
|
ast::ExprLit(li) => match li.node {
|
2012-10-24 11:21:57 -05:00
|
|
|
ast::lit_int(v, _) => v,
|
|
|
|
ast::lit_uint(v, _) => v as i64,
|
|
|
|
ast::lit_int_unsuffixed(v) => v,
|
|
|
|
_ => return true
|
|
|
|
},
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!()
|
2012-10-24 11:21:57 -05:00
|
|
|
};
|
2013-10-02 13:29:29 -05:00
|
|
|
is_valid(norm_binop, lit_val, min, max)
|
2012-10-24 11:21:57 -05:00
|
|
|
}
|
|
|
|
ty::ty_uint(uint_ty) => {
|
2013-10-02 13:29:29 -05:00
|
|
|
let (min, max): (u64, u64) = uint_ty_range(uint_ty);
|
2012-10-24 11:21:57 -05:00
|
|
|
let lit_val: u64 = match lit.node {
|
2013-10-02 13:29:29 -05:00
|
|
|
ast::ExprLit(li) => match li.node {
|
2012-10-24 11:21:57 -05:00
|
|
|
ast::lit_int(v, _) => v as u64,
|
|
|
|
ast::lit_uint(v, _) => v,
|
|
|
|
ast::lit_int_unsuffixed(v) => v as u64,
|
|
|
|
_ => return true
|
|
|
|
},
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!()
|
2012-10-24 11:21:57 -05:00
|
|
|
};
|
2013-10-02 13:29:29 -05:00
|
|
|
is_valid(norm_binop, lit_val, min, max)
|
2012-10-24 11:21:57 -05:00
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn is_comparison(binop: ast::BinOp) -> bool {
|
2012-10-24 11:21:57 -05:00
|
|
|
match binop {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiEq | ast::BiLt | ast::BiLe |
|
|
|
|
ast::BiNe | ast::BiGe | ast::BiGt => true,
|
2012-10-24 11:21:57 -05:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2013-08-14 03:24:42 -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) => {
|
2013-12-23 13:15:16 -06:00
|
|
|
let def_map = cx.tcx.def_map.borrow();
|
|
|
|
match def_map.get().get_copy(&id) {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefPrimTy(ast::ty_int(ast::ty_i)) => {
|
2013-06-22 00:46:27 -05:00
|
|
|
cx.span_lint(ctypes, ty.span,
|
|
|
|
"found rust type `int` in foreign module, while \
|
|
|
|
libc::c_int or libc::c_long should be used");
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefPrimTy(ast::ty_uint(ast::ty_u)) => {
|
2013-06-22 00:46:27 -05:00
|
|
|
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-06-02 15:03:35 -05:00
|
|
|
ast::DefTy(def_id) => {
|
|
|
|
if !adt::is_ffi_safe(cx.tcx, def_id) {
|
|
|
|
cx.span_lint(ctypes, ty.span,
|
|
|
|
"found enum type without foreign-function-safe \
|
|
|
|
representation annotation in foreign module");
|
2013-11-28 14:22:53 -06:00
|
|
|
// hmm... this message could be more helpful
|
2013-06-02 15:03:35 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-22 00:46:27 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
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-11-30 16:00:39 -06:00
|
|
|
check_ty(cx, input.ty);
|
2012-01-19 02:50:51 -06:00
|
|
|
}
|
2013-11-30 16:00:39 -06: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-11-30 16:00:39 -06:00
|
|
|
ast::foreign_item_fn(decl, _) => {
|
2013-06-22 00:46:27 -05:00
|
|
|
check_foreign_fn(cx, decl);
|
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::foreign_item_static(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-10-02 13:29:29 -05:00
|
|
|
fn check_heap_type(cx: &Context, span: Span, ty: ty::t) {
|
|
|
|
let xs = [managed_heap_memory, owned_heap_memory, heap_memory];
|
|
|
|
for &lint in xs.iter() {
|
|
|
|
if cx.get_level(lint) == allow { continue }
|
|
|
|
|
|
|
|
let mut n_box = 0;
|
|
|
|
let mut n_uniq = 0;
|
|
|
|
ty::fold_ty(cx.tcx, ty, |t| {
|
|
|
|
match ty::get(t).sty {
|
2013-12-12 02:02:26 -06:00
|
|
|
ty::ty_box(_) | ty::ty_estr(ty::vstore_box) |
|
|
|
|
ty::ty_evec(_, ty::vstore_box) |
|
|
|
|
ty::ty_trait(_, _, ty::BoxTraitStore, _, _) => {
|
|
|
|
n_box += 1;
|
|
|
|
}
|
|
|
|
ty::ty_uniq(_) | ty::ty_estr(ty::vstore_uniq) |
|
|
|
|
ty::ty_evec(_, ty::vstore_uniq) |
|
|
|
|
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) => {
|
|
|
|
n_uniq += 1;
|
|
|
|
}
|
|
|
|
ty::ty_closure(ref c) if c.sigil == ast::OwnedSigil => {
|
|
|
|
n_uniq += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => ()
|
2013-10-02 13:29:29 -05:00
|
|
|
};
|
|
|
|
t
|
|
|
|
});
|
2012-08-28 20:25:41 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
if n_uniq > 0 && lint != managed_heap_memory {
|
|
|
|
let s = ty_to_str(cx.tcx, ty);
|
|
|
|
let m = format!("type uses owned (~ type) pointers: {}", s);
|
|
|
|
cx.span_lint(lint, span, m);
|
|
|
|
}
|
2012-08-28 20:25:41 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
if n_box > 0 && lint != owned_heap_memory {
|
|
|
|
let s = ty_to_str(cx.tcx, ty);
|
|
|
|
let m = format!("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-10-02 13:29:29 -05:00
|
|
|
fn check_heap_item(cx: &Context, it: &ast::item) {
|
2013-05-07 01:07:00 -05:00
|
|
|
match it.node {
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::item_fn(..) |
|
|
|
|
ast::item_ty(..) |
|
|
|
|
ast::item_enum(..) |
|
|
|
|
ast::item_struct(..) => check_heap_type(cx, it.span,
|
2013-10-02 13:29:29 -05:00
|
|
|
ty::node_id_to_type(cx.tcx,
|
|
|
|
it.id)),
|
|
|
|
_ => ()
|
2013-05-07 01:07:00 -05:00
|
|
|
}
|
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-10-02 13:29:29 -05:00
|
|
|
check_heap_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-11-25 09:22:40 -06:00
|
|
|
static crate_attrs: &'static [&'static str] = &[
|
2013-12-26 00:20:43 -06:00
|
|
|
"crate_type", "feature", "no_uv", "no_main", "no_std", "crate_id",
|
2013-11-25 09:22:40 -06:00
|
|
|
"desc", "comment", "license", "copyright", // not used in rustc now
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
static obsolete_attrs: &'static [(&'static str, &'static str)] = &[
|
|
|
|
("abi", "Use `extern \"abi\" fn` instead"),
|
|
|
|
("auto_encode", "Use `#[deriving(Encodable)]` instead"),
|
|
|
|
("auto_decode", "Use `#[deriving(Decodable)]` instead"),
|
|
|
|
("fast_ffi", "Remove it"),
|
|
|
|
("fixed_stack_segment", "Remove it"),
|
|
|
|
("rust_stack", "Remove it"),
|
|
|
|
];
|
|
|
|
|
|
|
|
static other_attrs: &'static [&'static str] = &[
|
|
|
|
// item-level
|
|
|
|
"address_insignificant", // can be crate-level too
|
2013-11-05 23:38:08 -06:00
|
|
|
"thread_local", // for statics
|
2013-11-25 09:22:40 -06:00
|
|
|
"allow", "deny", "forbid", "warn", // lint options
|
|
|
|
"deprecated", "experimental", "unstable", "stable", "locked", "frozen", //item stability
|
|
|
|
"crate_map", "cfg", "doc", "export_name", "link_section", "no_freeze",
|
|
|
|
"no_mangle", "no_send", "static_assert", "unsafe_no_drop_flag",
|
2013-11-28 20:03:38 -06:00
|
|
|
"packed", "simd", "repr", "deriving", "unsafe_destructor", "link",
|
2013-11-25 09:22:40 -06:00
|
|
|
|
|
|
|
//mod-level
|
|
|
|
"path", "link_name", "link_args", "nolink", "macro_escape", "no_implicit_prelude",
|
|
|
|
|
|
|
|
// fn-level
|
|
|
|
"test", "bench", "should_fail", "ignore", "inline", "lang", "main", "start",
|
|
|
|
"no_split_stack", "cold",
|
|
|
|
|
|
|
|
// internal attribute: bypass privacy inside items
|
|
|
|
"!resolve_unexported",
|
|
|
|
];
|
|
|
|
|
|
|
|
fn check_crate_attrs_usage(cx: &Context, attrs: &[ast::Attribute]) {
|
|
|
|
|
|
|
|
for attr in attrs.iter() {
|
|
|
|
let name = attr.node.value.name();
|
|
|
|
let mut iter = crate_attrs.iter().chain(other_attrs.iter());
|
|
|
|
if !iter.any(|other_attr| { name.equiv(other_attr) }) {
|
|
|
|
cx.span_lint(attribute_usage, attr.span, "unknown crate attribute");
|
|
|
|
}
|
2013-12-20 18:20:54 -06:00
|
|
|
if name.equiv(& &"link") {
|
|
|
|
cx.tcx.sess.span_err(attr.span,
|
|
|
|
"obsolete crate `link` attribute");
|
|
|
|
cx.tcx.sess.note("the link attribute has been superceded by the crate_id \
|
|
|
|
attribute, which has the format `#[crate_id = \"name#version\"]`");
|
|
|
|
}
|
2013-11-25 09:22:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 00:21:51 -06:00
|
|
|
fn check_attrs_usage(cx: &Context, attrs: &[ast::Attribute]) {
|
|
|
|
// check if element has crate-level, obsolete, or any unknown attributes.
|
|
|
|
|
|
|
|
for attr in attrs.iter() {
|
2013-11-04 09:35:56 -06:00
|
|
|
let name = attr.node.value.name();
|
|
|
|
for crate_attr in crate_attrs.iter() {
|
|
|
|
if name.equiv(crate_attr) {
|
|
|
|
let msg = match attr.node.style {
|
2013-11-11 00:21:51 -06:00
|
|
|
ast::AttrOuter => "crate-level attribute should be an inner attribute: \
|
|
|
|
add semicolon at end",
|
2013-11-04 09:35:56 -06:00
|
|
|
ast::AttrInner => "crate-level attribute should be in the root module",
|
|
|
|
};
|
|
|
|
cx.span_lint(attribute_usage, attr.span, msg);
|
2013-11-11 00:21:51 -06:00
|
|
|
return;
|
2013-11-04 09:35:56 -06:00
|
|
|
}
|
|
|
|
}
|
2013-11-05 21:16:19 -06:00
|
|
|
|
|
|
|
for &(obs_attr, obs_alter) in obsolete_attrs.iter() {
|
|
|
|
if name.equiv(&obs_attr) {
|
|
|
|
cx.span_lint(attribute_usage, attr.span,
|
2013-11-25 08:17:01 -06:00
|
|
|
format!("obsolete attribute: {:s}", obs_alter));
|
2013-11-11 00:21:51 -06:00
|
|
|
return;
|
2013-11-05 21:16:19 -06:00
|
|
|
}
|
|
|
|
}
|
2013-11-11 00:21:51 -06:00
|
|
|
|
|
|
|
if !other_attrs.iter().any(|other_attr| { name.equiv(other_attr) }) {
|
|
|
|
cx.span_lint(attribute_usage, attr.span, "unknown attribute");
|
|
|
|
}
|
2013-11-04 09:35:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn check_heap_expr(cx: &Context, e: &ast::Expr) {
|
|
|
|
let ty = ty::expr_ty(cx.tcx, e);
|
|
|
|
check_heap_type(cx, e.span, ty);
|
2012-08-28 20:25:41 -05:00
|
|
|
}
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn check_path_statement(cx: &Context, s: &ast::Stmt) {
|
|
|
|
match s.node {
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::StmtSemi(@ast::Expr { node: ast::ExprPath(_), .. }, _) => {
|
2013-10-02 13:29:29 -05:00
|
|
|
cx.span_lint(path_statement, s.span,
|
|
|
|
"path statement with no effect");
|
|
|
|
}
|
|
|
|
_ => ()
|
2013-08-14 03:24:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 08:04:22 -05:00
|
|
|
fn check_item_non_camel_case_types(cx: &Context, it: &ast::item) {
|
2013-09-01 19:50:59 -05:00
|
|
|
fn is_camel_case(cx: ty::ctxt, ident: ast::Ident) -> bool {
|
2012-07-18 18:18:02 -05:00
|
|
|
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-09-01 19:50:59 -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,
|
2013-09-28 00:38:08 -05:00
|
|
|
format!("{} `{}` should have a camel case identifier",
|
2013-07-25 14:26:38 -05:00
|
|
|
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-11-28 14:22:53 -06:00
|
|
|
ast::item_ty(..) | ast::item_struct(..) => {
|
2013-07-25 14:26:38 -05:00
|
|
|
check_case(cx, "type", it.ident, it.span)
|
|
|
|
}
|
2013-11-28 14:22:53 -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
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::item_static(_, ast::MutImmutable, _) => {
|
2013-06-30 22:51:13 -05:00
|
|
|
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-11-23 04:18:51 -06:00
|
|
|
if s.chars().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-10-01 11:03:26 -05:00
|
|
|
fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
|
|
|
|
// Lint for constants that look like binding identifiers (#7526)
|
2013-12-23 13:15:16 -06:00
|
|
|
let def_map = cx.tcx.def_map.borrow();
|
|
|
|
match (&p.node, def_map.get().find(&p.id)) {
|
2013-10-01 11:03:26 -05:00
|
|
|
(&ast::PatIdent(_, ref path, _), Some(&ast::DefStatic(_, false))) => {
|
|
|
|
// last identifier alone is right choice for this lint.
|
|
|
|
let ident = path.segments.last().identifier;
|
|
|
|
let s = cx.tcx.sess.str_of(ident);
|
2013-11-23 04:18:51 -06:00
|
|
|
if s.chars().any(|c| c.is_lowercase()) {
|
2013-10-01 11:03:26 -05:00
|
|
|
cx.span_lint(non_uppercase_pattern_statics, path.span,
|
|
|
|
"static constant in pattern should be all caps");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
|
|
|
|
match e.node {
|
2013-11-21 16:36:54 -06:00
|
|
|
// Don't warn about generated blocks, that'll just pollute the output.
|
2013-10-02 13:29:29 -05:00
|
|
|
ast::ExprBlock(ref blk) => {
|
2013-12-20 19:46:04 -06:00
|
|
|
let used_unsafe = cx.tcx.used_unsafe.borrow();
|
2013-10-02 13:29:29 -05:00
|
|
|
if blk.rules == ast::UnsafeBlock(ast::UserProvided) &&
|
2013-12-20 19:46:04 -06:00
|
|
|
!used_unsafe.get().contains(&blk.id) {
|
2013-10-02 13:29:29 -05:00
|
|
|
cx.span_lint(unused_unsafe, blk.span,
|
|
|
|
"unnecessary `unsafe` block");
|
2013-04-12 00:09:54 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-02 13:29:29 -05:00
|
|
|
_ => ()
|
2013-05-27 22:21:29 -05:00
|
|
|
}
|
2013-08-14 03:24:42 -05:00
|
|
|
}
|
|
|
|
|
2013-11-21 16:36:54 -06:00
|
|
|
fn check_unsafe_block(cx: &Context, e: &ast::Expr) {
|
|
|
|
match e.node {
|
|
|
|
// Don't warn about generated blocks, that'll just pollute the output.
|
|
|
|
ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock(ast::UserProvided) => {
|
|
|
|
cx.span_lint(unsafe_block, blk.span, "usage of an `unsafe` block");
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 12:22:21 -06:00
|
|
|
fn check_unused_mut_pat(cx: &Context, p: &ast::Pat) {
|
2013-10-23 02:50:22 -05:00
|
|
|
match p.node {
|
2013-11-15 07:39:48 -06:00
|
|
|
ast::PatIdent(ast::BindByValue(ast::MutMutable),
|
|
|
|
ref path, _) if pat_util::pat_is_binding(cx.tcx.def_map, p)=> {
|
|
|
|
// `let mut _a = 1;` doesn't need a warning.
|
|
|
|
let initial_underscore = match path.segments {
|
2013-11-28 14:22:53 -06:00
|
|
|
[ast::PathSegment { identifier: id, .. }] => {
|
2013-11-15 07:39:48 -06:00
|
|
|
cx.tcx.sess.str_of(id).starts_with("_")
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
cx.tcx.sess.span_bug(p.span,
|
|
|
|
"mutable binding that doesn't \
|
|
|
|
consist of exactly one segment");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-21 19:28:21 -06:00
|
|
|
let used_mut_nodes = cx.tcx.used_mut_nodes.borrow();
|
|
|
|
if !initial_underscore && !used_mut_nodes.get().contains(&p.id) {
|
2013-11-15 07:39:48 -06:00
|
|
|
cx.span_lint(unused_mut, p.span,
|
|
|
|
"variable does not need to be mutable");
|
2013-10-20 07:31:23 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-23 02:50:22 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
2013-08-29 21:01:19 -05:00
|
|
|
}
|
|
|
|
|
2013-12-09 08:18:24 -06:00
|
|
|
enum Allocation {
|
|
|
|
VectorAllocation,
|
|
|
|
BoxAllocation
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn check_unnecessary_allocation(cx: &Context, e: &ast::Expr) {
|
2013-12-09 08:18:24 -06:00
|
|
|
// Warn if string and vector literals with sigils, or boxing expressions,
|
|
|
|
// are immediately borrowed.
|
|
|
|
let allocation = match e.node {
|
2013-10-02 13:29:29 -05:00
|
|
|
ast::ExprVstore(e2, ast::ExprVstoreUniq) |
|
|
|
|
ast::ExprVstore(e2, ast::ExprVstoreBox) => {
|
|
|
|
match e2.node {
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::ExprLit(@codemap::Spanned{node: ast::lit_str(..), ..}) |
|
2013-12-09 08:18:24 -06:00
|
|
|
ast::ExprVec(..) => VectorAllocation,
|
2013-10-02 13:29:29 -05:00
|
|
|
_ => return
|
2013-05-18 02:07:28 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-09 08:18:24 -06:00
|
|
|
ast::ExprUnary(_, ast::UnUniq, _) |
|
2013-12-31 14:55:39 -06:00
|
|
|
ast::ExprUnary(_, ast::UnBox, _) => BoxAllocation,
|
2013-05-18 02:07:28 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
_ => return
|
2013-12-09 08:18:24 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let report = |msg| {
|
|
|
|
cx.span_lint(unnecessary_allocation, e.span, msg);
|
|
|
|
};
|
2013-05-18 02:07:28 -05:00
|
|
|
|
2013-12-19 20:26:45 -06:00
|
|
|
let adjustment = {
|
|
|
|
let adjustments = cx.tcx.adjustments.borrow();
|
|
|
|
adjustments.get().find_copy(&e.id)
|
|
|
|
};
|
|
|
|
match adjustment {
|
2013-12-09 08:18:24 -06:00
|
|
|
Some(@ty::AutoDerefRef(ty::AutoDerefRef { autoref, .. })) => {
|
|
|
|
match (allocation, autoref) {
|
|
|
|
(VectorAllocation, Some(ty::AutoBorrowVec(..))) => {
|
|
|
|
report("unnecessary allocation, the sigil can be removed");
|
|
|
|
}
|
|
|
|
(BoxAllocation, Some(ty::AutoPtr(_, ast::MutImmutable))) => {
|
|
|
|
report("unnecessary allocation, use & instead");
|
|
|
|
}
|
|
|
|
(BoxAllocation, Some(ty::AutoPtr(_, ast::MutMutable))) => {
|
|
|
|
report("unnecessary allocation, use &mut instead");
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
2013-08-14 03:24:42 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
2013-05-18 02:07:28 -05:00
|
|
|
}
|
|
|
|
|
2013-11-11 22:17:47 -06:00
|
|
|
fn check_missing_doc_attrs(cx: &Context,
|
2013-11-26 00:12:14 -06:00
|
|
|
id: Option<ast::NodeId>,
|
2013-11-11 22:17:47 -06:00
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
sp: Span,
|
|
|
|
desc: &'static str) {
|
|
|
|
// 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 }
|
|
|
|
|
|
|
|
// `#[doc(hidden)]` disables missing_doc check.
|
|
|
|
if cx.is_doc_hidden { return }
|
|
|
|
|
2013-11-26 00:12:14 -06:00
|
|
|
// Only check publicly-visible items, using the result from the privacy pass. It's an option so
|
|
|
|
// the crate root can also use this function (it doesn't have a NodeId).
|
|
|
|
match id {
|
|
|
|
Some(ref id) if !cx.exported_items.contains(id) => return,
|
|
|
|
_ => ()
|
|
|
|
}
|
2013-11-11 22:17:47 -06:00
|
|
|
|
2013-12-08 20:41:24 -06:00
|
|
|
let has_doc = attrs.iter().any(|a| {
|
|
|
|
match a.node.value.node {
|
|
|
|
ast::MetaNameValue(ref name, _) if "doc" == *name => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if !has_doc {
|
2013-11-11 22:17:47 -06:00
|
|
|
cx.span_lint(missing_doc, sp,
|
|
|
|
format!("missing documentation for {}", desc));
|
2013-08-14 03:24:42 -05:00
|
|
|
}
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
2013-05-28 15:44:53 -05:00
|
|
|
|
2013-11-26 00:12:14 -06:00
|
|
|
fn check_missing_doc_item(cx: &Context, it: &ast::item) {
|
2013-11-11 22:17:47 -06:00
|
|
|
let desc = match it.node {
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::item_fn(..) => "a function",
|
|
|
|
ast::item_mod(..) => "a module",
|
|
|
|
ast::item_enum(..) => "an enum",
|
|
|
|
ast::item_struct(..) => "a struct",
|
|
|
|
ast::item_trait(..) => "a trait",
|
2013-11-11 22:17:47 -06:00
|
|
|
_ => return
|
|
|
|
};
|
2013-11-26 00:12:14 -06:00
|
|
|
check_missing_doc_attrs(cx, Some(it.id), it.attrs, it.span, desc);
|
2013-11-11 22:17:47 -06:00
|
|
|
}
|
2013-08-14 03:24:42 -05:00
|
|
|
|
2013-11-11 22:17:47 -06:00
|
|
|
fn check_missing_doc_method(cx: &Context, m: &ast::method) {
|
|
|
|
let did = ast::DefId {
|
|
|
|
crate: ast::LOCAL_CRATE,
|
|
|
|
node: m.id
|
|
|
|
};
|
2013-12-18 17:50:53 -06:00
|
|
|
|
|
|
|
let method_opt;
|
|
|
|
{
|
|
|
|
let methods = cx.tcx.methods.borrow();
|
|
|
|
method_opt = methods.get().find(&did).map(|method| *method);
|
|
|
|
}
|
|
|
|
|
|
|
|
match method_opt {
|
2013-11-11 22:17:47 -06:00
|
|
|
None => cx.tcx.sess.span_bug(m.span, "missing method descriptor?!"),
|
|
|
|
Some(md) => {
|
|
|
|
match md.container {
|
|
|
|
// Always check default methods defined on traits.
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::TraitContainer(..) => {}
|
2013-11-11 22:17:47 -06:00
|
|
|
// For methods defined on impls, it depends on whether
|
|
|
|
// it is an implementation for a trait or is a plain
|
|
|
|
// impl.
|
|
|
|
ty::ImplContainer(cid) => {
|
|
|
|
match ty::impl_trait_ref(cx.tcx, cid) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(..) => return, // impl for trait: don't doc
|
2013-11-11 22:17:47 -06:00
|
|
|
None => {} // plain impl: doc according to privacy
|
|
|
|
}
|
2013-05-24 07:13:41 -05:00
|
|
|
}
|
2013-05-28 15:44:53 -05:00
|
|
|
}
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
2013-08-14 03:24:42 -05:00
|
|
|
}
|
2013-11-26 00:12:14 -06:00
|
|
|
check_missing_doc_attrs(cx, Some(m.id), m.attrs, m.span, "a method");
|
2013-11-11 22:17:47 -06:00
|
|
|
}
|
2013-08-14 03:24:42 -05:00
|
|
|
|
2013-11-11 22:17:47 -06:00
|
|
|
fn check_missing_doc_ty_method(cx: &Context, tm: &ast::TypeMethod) {
|
2013-11-26 00:12:14 -06:00
|
|
|
check_missing_doc_attrs(cx, Some(tm.id), tm.attrs, tm.span, "a type method");
|
2013-11-11 22:17:47 -06:00
|
|
|
}
|
2013-08-14 03:24:42 -05:00
|
|
|
|
2013-11-11 22:17:47 -06:00
|
|
|
fn check_missing_doc_struct_field(cx: &Context, sf: &ast::struct_field) {
|
|
|
|
match sf.node.kind {
|
|
|
|
ast::named_field(_, vis) if vis != ast::private =>
|
2013-11-26 00:12:14 -06:00
|
|
|
check_missing_doc_attrs(cx, Some(cx.cur_struct_def_id), sf.node.attrs,
|
2013-11-11 22:17:47 -06:00
|
|
|
sf.span, "a struct field"),
|
|
|
|
_ => {}
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
2013-08-14 03:24:42 -05:00
|
|
|
}
|
|
|
|
|
2013-11-11 22:17:47 -06:00
|
|
|
fn check_missing_doc_variant(cx: &Context, v: &ast::variant) {
|
2013-11-26 00:12:14 -06:00
|
|
|
check_missing_doc_attrs(cx, Some(v.node.id), v.node.attrs, v.span, "a variant");
|
2013-11-11 22:17:47 -06:00
|
|
|
}
|
|
|
|
|
2013-08-31 02:13:57 -05:00
|
|
|
/// Checks for use of items with #[deprecated], #[experimental] and
|
|
|
|
/// #[unstable] (or none of them) attributes.
|
2013-10-02 13:29:29 -05:00
|
|
|
fn check_stability(cx: &Context, e: &ast::Expr) {
|
2013-12-15 20:52:48 -06:00
|
|
|
let id = match e.node {
|
|
|
|
ast::ExprPath(..) | ast::ExprStruct(..) => {
|
2013-12-23 13:15:16 -06:00
|
|
|
let def_map = cx.tcx.def_map.borrow();
|
|
|
|
match def_map.get().find(&e.id) {
|
2013-12-15 20:52:48 -06:00
|
|
|
Some(&def) => ast_util::def_id_of_def(def),
|
|
|
|
None => return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::ExprMethodCall(..) => {
|
2013-12-21 19:04:42 -06:00
|
|
|
let method_map = cx.method_map.borrow();
|
|
|
|
match method_map.get().find(&e.id) {
|
2013-12-15 20:52:48 -06:00
|
|
|
Some(&typeck::method_map_entry { origin, .. }) => {
|
|
|
|
match origin {
|
|
|
|
typeck::method_static(def_id) => {
|
|
|
|
// If this implements a trait method, get def_id
|
|
|
|
// of the method inside trait definition.
|
|
|
|
// Otherwise, use the current def_id (which refers
|
|
|
|
// to the method inside impl).
|
|
|
|
ty::trait_method_of_method(
|
|
|
|
cx.tcx, def_id).unwrap_or(def_id)
|
|
|
|
}
|
|
|
|
typeck::method_param(typeck::method_param {
|
|
|
|
trait_id: trait_id,
|
|
|
|
method_num: index,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| typeck::method_object(typeck::method_object {
|
|
|
|
trait_id: trait_id,
|
|
|
|
method_num: index,
|
|
|
|
..
|
|
|
|
}) => ty::trait_method(cx.tcx, trait_id, index).def_id
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 13:29:29 -05:00
|
|
|
None => return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return
|
|
|
|
};
|
|
|
|
|
|
|
|
let stability = if ast_util::is_local(id) {
|
|
|
|
// this crate
|
2013-12-27 18:09:29 -06:00
|
|
|
let items = cx.tcx.items.borrow();
|
|
|
|
match items.get().find(&id.node) {
|
2013-10-02 13:29:29 -05:00
|
|
|
Some(ast_node) => {
|
2013-11-21 17:42:55 -06:00
|
|
|
let s = ast_node.with_attrs(|attrs| {
|
|
|
|
attrs.map(|a| {
|
2013-10-02 13:29:29 -05:00
|
|
|
attr::find_stability(a.iter().map(|a| a.meta()))
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
|
|
|
});
|
2013-10-02 13:29:29 -05:00
|
|
|
match s {
|
|
|
|
Some(s) => s,
|
|
|
|
|
|
|
|
// no possibility of having attributes
|
|
|
|
// (e.g. it's a local variable), so just
|
|
|
|
// ignore it.
|
|
|
|
None => return
|
2013-08-31 02:13:57 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-15 20:52:48 -06:00
|
|
|
_ => cx.tcx.sess.span_bug(e.span,
|
|
|
|
format!("handle_def: {:?} not found", id))
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// cross-crate
|
|
|
|
|
|
|
|
let mut s = None;
|
|
|
|
// run through all the attributes and take the first
|
|
|
|
// stability one.
|
2013-11-21 17:42:55 -06:00
|
|
|
csearch::get_item_attrs(cx.tcx.cstore, id, |meta_items| {
|
2013-10-02 13:29:29 -05:00
|
|
|
if s.is_none() {
|
|
|
|
s = attr::find_stability(meta_items.move_iter())
|
2013-08-31 02:13:57 -05:00
|
|
|
}
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2013-10-02 13:29:29 -05:00
|
|
|
s
|
|
|
|
};
|
2013-08-31 02:13:57 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
let (lint, label) = match stability {
|
|
|
|
// no stability attributes == Unstable
|
|
|
|
None => (unstable, "unmarked"),
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(attr::Stability { level: attr::Unstable, .. }) =>
|
2013-10-02 13:29:29 -05:00
|
|
|
(unstable, "unstable"),
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(attr::Stability { level: attr::Experimental, .. }) =>
|
2013-10-02 13:29:29 -05:00
|
|
|
(experimental, "experimental"),
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(attr::Stability { level: attr::Deprecated, .. }) =>
|
2013-10-02 13:29:29 -05:00
|
|
|
(deprecated, "deprecated"),
|
|
|
|
_ => return
|
|
|
|
};
|
2013-08-31 02:13:57 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
let msg = match stability {
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(attr::Stability { text: Some(ref s), .. }) => {
|
2013-10-02 13:29:29 -05:00
|
|
|
format!("use of {} item: {}", label, *s)
|
|
|
|
}
|
|
|
|
_ => format!("use of {} item", label)
|
|
|
|
};
|
2013-08-31 02:13:57 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
cx.span_lint(lint, e.span, msg);
|
2013-08-31 02:13:57 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Visitor<()> for Context<'a> {
|
2013-10-02 13:29:29 -05:00
|
|
|
fn visit_item(&mut self, it: @ast::item, _: ()) {
|
2013-11-21 17:42:55 -06:00
|
|
|
self.with_lint_attrs(it.attrs, |cx| {
|
2013-10-02 13:29:29 -05:00
|
|
|
check_item_ctypes(cx, it);
|
|
|
|
check_item_non_camel_case_types(cx, it);
|
|
|
|
check_item_non_uppercase_statics(cx, it);
|
|
|
|
check_heap_item(cx, it);
|
2013-11-11 22:17:47 -06:00
|
|
|
check_missing_doc_item(cx, it);
|
2013-11-11 00:21:51 -06:00
|
|
|
check_attrs_usage(cx, it.attrs);
|
2013-10-02 13:29:29 -05:00
|
|
|
|
2013-11-21 17:42:55 -06:00
|
|
|
cx.visit_ids(|v| v.visit_item(it, ()));
|
2013-08-31 02:13:57 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
visit::walk_item(cx, it, ());
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2013-08-31 02:13:57 -05:00
|
|
|
}
|
|
|
|
|
2013-11-11 00:21:51 -06:00
|
|
|
fn visit_foreign_item(&mut self, it: @ast::foreign_item, _: ()) {
|
2013-11-24 13:44:28 -06:00
|
|
|
self.with_lint_attrs(it.attrs, |cx| {
|
2013-11-11 00:21:51 -06:00
|
|
|
check_attrs_usage(cx, it.attrs);
|
|
|
|
visit::walk_foreign_item(cx, it, ());
|
2013-11-24 13:44:28 -06:00
|
|
|
})
|
2013-11-11 00:21:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_view_item(&mut self, i: &ast::view_item, _: ()) {
|
2013-11-24 13:44:28 -06:00
|
|
|
self.with_lint_attrs(i.attrs, |cx| {
|
2013-11-11 00:21:51 -06:00
|
|
|
check_attrs_usage(cx, i.attrs);
|
|
|
|
visit::walk_view_item(cx, i, ());
|
2013-11-24 13:44:28 -06:00
|
|
|
})
|
2013-11-11 00:21:51 -06:00
|
|
|
}
|
|
|
|
|
2013-11-25 12:22:21 -06:00
|
|
|
fn visit_pat(&mut self, p: &ast::Pat, _: ()) {
|
2013-10-02 13:29:29 -05:00
|
|
|
check_pat_non_uppercase_statics(self, p);
|
2013-10-20 07:31:23 -05:00
|
|
|
check_unused_mut_pat(self, p);
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
visit::walk_pat(self, p, ());
|
2013-08-31 02:13:57 -05:00
|
|
|
}
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn visit_expr(&mut self, e: @ast::Expr, _: ()) {
|
2013-10-27 13:59:58 -05:00
|
|
|
match e.node {
|
|
|
|
ast::ExprUnary(_, ast::UnNeg, expr) => {
|
|
|
|
// propagate negation, if the negation itself isn't negated
|
|
|
|
if self.negated_expr_id != e.id {
|
|
|
|
self.negated_expr_id = expr.id;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::ExprParen(expr) => if self.negated_expr_id == e.id {
|
|
|
|
self.negated_expr_id = expr.id
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
};
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
check_while_true_expr(self, e);
|
|
|
|
check_stability(self, e);
|
|
|
|
check_unused_unsafe(self, e);
|
2013-11-21 16:36:54 -06:00
|
|
|
check_unsafe_block(self, e);
|
2013-10-02 13:29:29 -05:00
|
|
|
check_unnecessary_allocation(self, e);
|
|
|
|
check_heap_expr(self, e);
|
2013-10-27 13:59:58 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
check_type_limits(self, e);
|
2013-08-31 02:13:57 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
visit::walk_expr(self, e, ());
|
2013-08-31 02:13:57 -05:00
|
|
|
}
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn visit_stmt(&mut self, s: @ast::Stmt, _: ()) {
|
|
|
|
check_path_statement(self, s);
|
2013-08-31 02:13:57 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
visit::walk_stmt(self, s, ());
|
|
|
|
}
|
2013-08-31 02:13:57 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
fn visit_fn(&mut self, fk: &visit::fn_kind, decl: &ast::fn_decl,
|
2013-11-30 16:00:39 -06:00
|
|
|
body: ast::P<ast::Block>, span: Span, id: ast::NodeId, _: ()) {
|
2013-10-02 13:29:29 -05:00
|
|
|
let recurse = |this: &mut Context| {
|
|
|
|
visit::walk_fn(this, fk, decl, body, span, id, ());
|
|
|
|
};
|
2013-08-14 03:24:42 -05:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
match *fk {
|
|
|
|
visit::fk_method(_, _, m) => {
|
2013-11-21 17:42:55 -06:00
|
|
|
self.with_lint_attrs(m.attrs, |cx| {
|
2013-11-11 22:17:47 -06:00
|
|
|
check_missing_doc_method(cx, m);
|
2013-11-11 00:21:51 -06:00
|
|
|
check_attrs_usage(cx, m.attrs);
|
2013-11-11 22:17:47 -06:00
|
|
|
|
2013-11-21 17:42:55 -06:00
|
|
|
cx.visit_ids(|v| {
|
2013-10-02 13:29:29 -05:00
|
|
|
v.visit_fn(fk, decl, body, span, id, ());
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2013-10-02 13:29:29 -05:00
|
|
|
recurse(cx);
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
|
|
|
_ => recurse(self),
|
|
|
|
}
|
2013-08-14 03:24:42 -05:00
|
|
|
}
|
2013-11-11 22:17:47 -06:00
|
|
|
|
2013-11-11 00:21:51 -06:00
|
|
|
|
2013-11-11 22:17:47 -06:00
|
|
|
fn visit_ty_method(&mut self, t: &ast::TypeMethod, _: ()) {
|
2013-11-21 17:42:55 -06:00
|
|
|
self.with_lint_attrs(t.attrs, |cx| {
|
2013-11-11 22:17:47 -06:00
|
|
|
check_missing_doc_ty_method(cx, t);
|
2013-11-11 00:21:51 -06:00
|
|
|
check_attrs_usage(cx, t.attrs);
|
2013-11-11 22:17:47 -06:00
|
|
|
|
|
|
|
visit::walk_ty_method(cx, t, ());
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2013-11-11 22:17:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_struct_def(&mut self,
|
|
|
|
s: @ast::struct_def,
|
|
|
|
i: ast::Ident,
|
|
|
|
g: &ast::Generics,
|
|
|
|
id: ast::NodeId,
|
|
|
|
_: ()) {
|
|
|
|
let old_id = self.cur_struct_def_id;
|
|
|
|
self.cur_struct_def_id = id;
|
|
|
|
visit::walk_struct_def(self, s, i, g, id, ());
|
|
|
|
self.cur_struct_def_id = old_id;
|
|
|
|
}
|
|
|
|
|
2013-11-30 10:27:25 -06:00
|
|
|
fn visit_struct_field(&mut self, s: &ast::struct_field, _: ()) {
|
2013-11-21 17:42:55 -06:00
|
|
|
self.with_lint_attrs(s.node.attrs, |cx| {
|
2013-11-11 22:17:47 -06:00
|
|
|
check_missing_doc_struct_field(cx, s);
|
2013-11-11 00:21:51 -06:00
|
|
|
check_attrs_usage(cx, s.node.attrs);
|
2013-11-11 22:17:47 -06:00
|
|
|
|
|
|
|
visit::walk_struct_field(cx, s, ());
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2013-11-11 22:17:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_variant(&mut self, v: &ast::variant, g: &ast::Generics, _: ()) {
|
2013-11-21 17:42:55 -06:00
|
|
|
self.with_lint_attrs(v.node.attrs, |cx| {
|
2013-11-11 22:17:47 -06:00
|
|
|
check_missing_doc_variant(cx, v);
|
2013-11-11 00:21:51 -06:00
|
|
|
check_attrs_usage(cx, v.node.attrs);
|
2013-11-11 22:17:47 -06:00
|
|
|
|
|
|
|
visit::walk_variant(cx, v, g, ());
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2013-11-11 22:17:47 -06:00
|
|
|
}
|
2013-12-08 13:25:35 -06:00
|
|
|
|
|
|
|
// FIXME(#10894) should continue recursing
|
|
|
|
fn visit_ty(&mut self, _t: &ast::Ty, _: ()) {}
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
2013-05-28 15:44:53 -05:00
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> IdVisitingOperation for Context<'a> {
|
2013-10-02 13:29:29 -05:00
|
|
|
fn visit_id(&self, id: ast::NodeId) {
|
2013-12-20 22:04:17 -06:00
|
|
|
let mut lints = self.tcx.sess.lints.borrow_mut();
|
|
|
|
match lints.get().pop(&id) {
|
2013-10-02 13:29:29 -05:00
|
|
|
None => {}
|
|
|
|
Some(l) => {
|
|
|
|
for (lint, span, msg) in l.move_iter() {
|
|
|
|
self.span_lint(lint, span, msg)
|
2013-08-14 03:24:42 -05:00
|
|
|
}
|
2013-10-02 13:29:29 -05:00
|
|
|
}
|
|
|
|
}
|
2013-08-14 03:24:42 -05:00
|
|
|
}
|
2013-05-24 03:27:31 -05:00
|
|
|
}
|
|
|
|
|
2013-11-11 22:17:47 -06:00
|
|
|
pub fn check_crate(tcx: ty::ctxt,
|
2013-12-15 20:52:48 -06:00
|
|
|
method_map: typeck::method_map,
|
2013-11-11 22:17:47 -06:00
|
|
|
exported_items: &privacy::ExportedItems,
|
|
|
|
crate: &ast::Crate) {
|
2013-10-02 13:29:29 -05:00
|
|
|
let mut cx = Context {
|
2013-04-30 00:15:17 -05:00
|
|
|
dict: @get_lint_dict(),
|
2013-10-02 13:29:29 -05:00
|
|
|
cur: SmallIntMap::new(),
|
2013-04-30 00:15:17 -05:00
|
|
|
tcx: tcx,
|
2013-12-15 20:52:48 -06:00
|
|
|
method_map: method_map,
|
2013-11-11 22:17:47 -06:00
|
|
|
exported_items: exported_items,
|
|
|
|
cur_struct_def_id: -1,
|
|
|
|
is_doc_hidden: false,
|
2013-04-30 00:15:17 -05:00
|
|
|
lint_stack: ~[],
|
2013-10-27 13:59:58 -05:00
|
|
|
negated_expr_id: -1
|
2013-04-30 00:15:17 -05:00
|
|
|
};
|
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
// Install default lint levels, followed by the command line levels, and
|
|
|
|
// then actually visit the whole crate.
|
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
|
|
|
}
|
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-11-21 17:42:55 -06:00
|
|
|
cx.with_lint_attrs(crate.attrs, |cx| {
|
2013-11-26 16:55:06 -06:00
|
|
|
cx.visit_id(ast::CRATE_NODE_ID);
|
2013-11-21 17:42:55 -06:00
|
|
|
cx.visit_ids(|v| {
|
2013-10-02 13:29:29 -05:00
|
|
|
v.visited_outermost = true;
|
|
|
|
visit::walk_crate(v, crate, ());
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2013-11-25 09:22:40 -06:00
|
|
|
|
|
|
|
check_crate_attrs_usage(cx, crate.attrs);
|
2013-11-26 00:12:14 -06:00
|
|
|
// since the root module isn't visited as an item (because it isn't an item), warn for it
|
|
|
|
// here.
|
|
|
|
check_missing_doc_attrs(cx, None, crate.attrs, crate.span, "crate");
|
2013-11-25 09:22:40 -06:00
|
|
|
|
2013-10-02 13:29:29 -05:00
|
|
|
visit::walk_crate(cx, crate, ());
|
2013-11-21 17:42:55 -06: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-12-20 22:04:17 -06:00
|
|
|
let lints = tcx.sess.lints.borrow();
|
|
|
|
for (id, v) in lints.get().iter() {
|
2013-10-02 13:29:29 -05:00
|
|
|
for &(lint, span, ref msg) in v.iter() {
|
|
|
|
tcx.sess.span_bug(span, format!("unprocessed lint {:?} at {}: {}",
|
|
|
|
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
|
|
|
}
|