2015-02-25 05:44:44 -06:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-02-25 06:03:44 -06:00
|
|
|
//! Lints in the Rust compiler.
|
2015-02-25 05:44:44 -06:00
|
|
|
//!
|
2015-02-25 06:03:44 -06:00
|
|
|
//! This contains lints which can feasibly be implemented as their own
|
|
|
|
//! AST visitor. Also see `rustc::lint::builtin`, which contains the
|
|
|
|
//! definitions of lints that are emitted directly inside the main
|
|
|
|
//! compiler.
|
2015-02-25 05:44:44 -06:00
|
|
|
//!
|
|
|
|
//! To add a new lint to rustc, declare it here using `declare_lint!()`.
|
|
|
|
//! Then add code to emit the new lint in the appropriate circumstances.
|
2015-02-25 06:03:44 -06:00
|
|
|
//! You can do that in an existing `LintPass` if it makes sense, or in a
|
|
|
|
//! new `LintPass`, or using `Session::add_lint` elsewhere in the
|
|
|
|
//! compiler. Only do the latter if the check can't be written cleanly as a
|
|
|
|
//! `LintPass` (also, note that such lints will need to be defined in
|
|
|
|
//! `rustc::lint::builtin`, not here).
|
2015-02-25 05:44:44 -06:00
|
|
|
//!
|
|
|
|
//! If you define a new `LintPass`, you will also need to add it to the
|
2015-02-25 06:03:44 -06:00
|
|
|
//! `add_builtin!` or `add_builtin_with_new!` invocation in `lib.rs`.
|
2015-02-25 05:44:44 -06:00
|
|
|
//! Use the former for unit-like structs and the latter for structs with
|
|
|
|
//! a `pub fn new()`.
|
|
|
|
|
2016-03-29 04:54:26 -05:00
|
|
|
use rustc::hir::def::Def;
|
|
|
|
use rustc::hir::def_id::DefId;
|
2016-03-10 18:31:38 -06:00
|
|
|
use rustc::cfg;
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::subst::Substs;
|
2017-06-07 07:21:55 -05:00
|
|
|
use rustc::ty::{self, Ty};
|
2016-06-30 13:22:47 -05:00
|
|
|
use rustc::traits::{self, Reveal};
|
2016-03-29 00:50:44 -05:00
|
|
|
use rustc::hir::map as hir_map;
|
2016-10-08 23:08:07 -05:00
|
|
|
use util::nodemap::NodeSet;
|
2016-11-10 11:08:21 -06:00
|
|
|
use lint::{Level, LateContext, LintContext, LintArray};
|
2016-10-10 21:51:27 -05:00
|
|
|
use lint::{LintPass, LateLintPass, EarlyLintPass, EarlyContext};
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2015-08-11 19:27:05 -05:00
|
|
|
use std::collections::HashSet;
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-10-18 00:04:28 -05:00
|
|
|
use syntax::ast;
|
2016-08-22 22:54:53 -05:00
|
|
|
use syntax::attr;
|
2016-10-18 00:04:28 -05:00
|
|
|
use syntax::feature_gate::{AttributeGate, AttributeType, Stability, deprecated_attributes};
|
2016-10-08 23:08:07 -05:00
|
|
|
use syntax_pos::Span;
|
2017-05-01 21:38:46 -05:00
|
|
|
use syntax::symbol::keywords;
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-03-29 00:50:44 -05:00
|
|
|
use rustc::hir::{self, PatKind};
|
|
|
|
use rustc::hir::intravisit::FnKind;
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2015-09-14 21:36:39 -05:00
|
|
|
use bad_style::{MethodLateContext, method_context};
|
|
|
|
|
2015-02-25 05:44:44 -06:00
|
|
|
// hardwired lints from librustc
|
|
|
|
pub use lint::builtin::*;
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
WHILE_TRUE,
|
|
|
|
Warn,
|
|
|
|
"suggest using `loop { }` instead of `while true { }`"
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 05:44:44 -06:00
|
|
|
pub struct WhileTrue;
|
|
|
|
|
|
|
|
impl LintPass for WhileTrue {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(WHILE_TRUE)
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for WhileTrue {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
|
2016-08-26 11:23:42 -05:00
|
|
|
if let hir::ExprWhile(ref cond, ..) = e.node {
|
2015-09-03 08:29:56 -05:00
|
|
|
if let hir::ExprLit(ref lit) = cond.node {
|
2016-02-08 10:06:20 -06:00
|
|
|
if let ast::LitKind::Bool(true) = lit.node {
|
2016-10-08 23:08:07 -05:00
|
|
|
cx.span_lint(WHILE_TRUE,
|
|
|
|
e.span,
|
2015-02-25 05:44:44 -06:00
|
|
|
"denote infinite loops with loop { ... }");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
BOX_POINTERS,
|
|
|
|
Allow,
|
|
|
|
"use of owned (Box type) heap memory"
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 05:44:44 -06:00
|
|
|
pub struct BoxPointers;
|
|
|
|
|
|
|
|
impl BoxPointers {
|
2016-12-07 06:56:36 -06:00
|
|
|
fn check_heap_type<'a, 'tcx>(&self, cx: &LateContext, span: Span, ty: Ty) {
|
2015-06-25 15:42:17 -05:00
|
|
|
for leaf_ty in ty.walk() {
|
2017-01-21 08:40:31 -06:00
|
|
|
if leaf_ty.is_box() {
|
2015-06-25 15:42:17 -05:00
|
|
|
let m = format!("type uses owned (Box type) pointers: {}", ty);
|
|
|
|
cx.span_lint(BOX_POINTERS, span, &m);
|
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for BoxPointers {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(BOX_POINTERS)
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxPointers {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
|
2015-02-25 05:44:44 -06:00
|
|
|
match it.node {
|
2015-09-03 08:29:56 -05:00
|
|
|
hir::ItemFn(..) |
|
|
|
|
hir::ItemTy(..) |
|
|
|
|
hir::ItemEnum(..) |
|
2016-08-10 13:00:17 -05:00
|
|
|
hir::ItemStruct(..) |
|
2016-10-26 20:52:10 -05:00
|
|
|
hir::ItemUnion(..) => {
|
2017-01-25 18:41:06 -06:00
|
|
|
let def_id = cx.tcx.hir.local_def_id(it.id);
|
2017-04-24 07:20:46 -05:00
|
|
|
self.check_heap_type(cx, it.span, cx.tcx.type_of(def_id))
|
2016-10-26 20:52:10 -05:00
|
|
|
}
|
2016-11-10 08:49:53 -06:00
|
|
|
_ => ()
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a struct, we also have to check the fields' types
|
|
|
|
match it.node {
|
2016-08-10 13:00:17 -05:00
|
|
|
hir::ItemStruct(ref struct_def, _) |
|
|
|
|
hir::ItemUnion(ref struct_def, _) => {
|
2015-10-08 15:45:46 -05:00
|
|
|
for struct_field in struct_def.fields() {
|
2017-01-25 18:41:06 -06:00
|
|
|
let def_id = cx.tcx.hir.local_def_id(struct_field.id);
|
2016-11-10 08:49:53 -06:00
|
|
|
self.check_heap_type(cx, struct_field.span,
|
2017-04-24 07:20:46 -05:00
|
|
|
cx.tcx.type_of(def_id));
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
_ => (),
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
|
2017-01-06 13:54:24 -06:00
|
|
|
let ty = cx.tables.node_id_to_type(e.id);
|
2015-02-25 05:44:44 -06:00
|
|
|
self.check_heap_type(cx, e.span, ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
NON_SHORTHAND_FIELD_PATTERNS,
|
|
|
|
Warn,
|
|
|
|
"using `Struct { x: x }` instead of `Struct { x }`"
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 05:44:44 -06:00
|
|
|
pub struct NonShorthandFieldPatterns;
|
|
|
|
|
|
|
|
impl LintPass for NonShorthandFieldPatterns {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(NON_SHORTHAND_FIELD_PATTERNS)
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_pat(&mut self, cx: &LateContext, pat: &hir::Pat) {
|
2016-06-03 15:15:00 -05:00
|
|
|
if let PatKind::Struct(_, ref field_pats, _) = pat.node {
|
|
|
|
for fieldpat in field_pats {
|
2015-02-28 06:31:14 -06:00
|
|
|
if fieldpat.node.is_shorthand {
|
2016-06-03 15:15:00 -05:00
|
|
|
continue;
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
2016-11-25 05:21:19 -06:00
|
|
|
if let PatKind::Binding(_, _, ident, None) = fieldpat.node.pat.node {
|
2016-06-10 16:12:39 -05:00
|
|
|
if ident.node == fieldpat.node.name {
|
2016-10-08 23:08:07 -05:00
|
|
|
cx.span_lint(NON_SHORTHAND_FIELD_PATTERNS,
|
|
|
|
fieldpat.span,
|
2015-02-25 05:44:44 -06:00
|
|
|
&format!("the `{}:` in this pattern is redundant and can \
|
2016-10-08 23:08:07 -05:00
|
|
|
be removed",
|
|
|
|
ident.node))
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
UNSAFE_CODE,
|
|
|
|
Allow,
|
|
|
|
"usage of `unsafe` code"
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 05:44:44 -06:00
|
|
|
pub struct UnsafeCode;
|
|
|
|
|
|
|
|
impl LintPass for UnsafeCode {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNSAFE_CODE)
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnsafeCode {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
|
2015-09-03 08:29:56 -05:00
|
|
|
if let hir::ExprBlock(ref blk) = e.node {
|
2015-02-25 05:44:44 -06:00
|
|
|
// Don't warn about generated blocks, that'll just pollute the output.
|
2015-09-03 08:29:56 -05:00
|
|
|
if blk.rules == hir::UnsafeBlock(hir::UserProvided) {
|
2015-02-25 05:44:44 -06:00
|
|
|
cx.span_lint(UNSAFE_CODE, blk.span, "usage of an `unsafe` block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
|
2015-02-25 05:44:44 -06:00
|
|
|
match it.node {
|
2016-10-08 23:08:07 -05:00
|
|
|
hir::ItemTrait(hir::Unsafety::Unsafe, ..) => {
|
|
|
|
cx.span_lint(UNSAFE_CODE, it.span, "declaration of an `unsafe` trait")
|
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-10-08 23:08:07 -05:00
|
|
|
hir::ItemImpl(hir::Unsafety::Unsafe, ..) => {
|
|
|
|
cx.span_lint(UNSAFE_CODE, it.span, "implementation of an `unsafe` trait")
|
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 23:08:07 -05:00
|
|
|
fn check_fn(&mut self,
|
|
|
|
cx: &LateContext,
|
2016-12-07 06:56:36 -06:00
|
|
|
fk: FnKind<'tcx>,
|
2016-10-08 23:08:07 -05:00
|
|
|
_: &hir::FnDecl,
|
2016-12-21 04:32:59 -06:00
|
|
|
_: &hir::Body,
|
2016-10-08 23:08:07 -05:00
|
|
|
span: Span,
|
|
|
|
_: ast::NodeId) {
|
2015-02-25 05:44:44 -06:00
|
|
|
match fk {
|
2016-10-08 23:08:07 -05:00
|
|
|
FnKind::ItemFn(_, _, hir::Unsafety::Unsafe, ..) => {
|
|
|
|
cx.span_lint(UNSAFE_CODE, span, "declaration of an `unsafe` function")
|
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-08-26 11:23:42 -05:00
|
|
|
FnKind::Method(_, sig, ..) => {
|
2015-09-03 08:29:56 -05:00
|
|
|
if sig.unsafety == hir::Unsafety::Unsafe {
|
2015-03-10 05:28:44 -05:00
|
|
|
cx.span_lint(UNSAFE_CODE, span, "implementation of an `unsafe` method")
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 14:46:11 -06:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext, item: &hir::TraitItem) {
|
|
|
|
if let hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(_)) = item.node {
|
2015-09-03 08:29:56 -05:00
|
|
|
if sig.unsafety == hir::Unsafety::Unsafe {
|
2016-10-08 23:08:07 -05:00
|
|
|
cx.span_lint(UNSAFE_CODE,
|
2016-12-20 14:46:11 -06:00
|
|
|
item.span,
|
2015-03-10 05:28:44 -05:00
|
|
|
"declaration of an `unsafe` method")
|
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
MISSING_DOCS,
|
|
|
|
Allow,
|
|
|
|
"detects missing documentation for public members"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MissingDoc {
|
|
|
|
/// Stack of whether #[doc(hidden)] is set
|
|
|
|
/// at each level which has lint attributes.
|
|
|
|
doc_hidden_stack: Vec<bool>,
|
2015-03-29 22:41:54 -05:00
|
|
|
|
|
|
|
/// Private traits or trait items that leaked through. Don't check their methods.
|
|
|
|
private_traits: HashSet<ast::NodeId>,
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MissingDoc {
|
|
|
|
pub fn new() -> MissingDoc {
|
|
|
|
MissingDoc {
|
2016-10-08 23:08:07 -05:00
|
|
|
doc_hidden_stack: vec![false],
|
2015-03-29 22:41:54 -05:00
|
|
|
private_traits: HashSet::new(),
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn doc_hidden(&self) -> bool {
|
|
|
|
*self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_missing_docs_attrs(&self,
|
2016-10-08 23:08:07 -05:00
|
|
|
cx: &LateContext,
|
|
|
|
id: Option<ast::NodeId>,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
sp: Span,
|
|
|
|
desc: &'static str) {
|
2015-02-25 05:44:44 -06:00
|
|
|
// If we're building a test harness, then warning about
|
|
|
|
// documentation is probably not really relevant right now.
|
2015-02-28 06:31:14 -06:00
|
|
|
if cx.sess().opts.test {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
|
|
|
// `#[doc(hidden)]` disables missing_docs check.
|
2015-02-28 06:31:14 -06:00
|
|
|
if self.doc_hidden() {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-25 05:44:44 -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).
|
2015-11-19 05:16:35 -06:00
|
|
|
if let Some(id) = id {
|
|
|
|
if !cx.access_levels.is_exported(id) {
|
2015-02-25 05:44:44 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 03:23:59 -06:00
|
|
|
let has_doc = attrs.iter().any(|a| a.is_value_str() && a.check_name("doc"));
|
2015-02-25 05:44:44 -06:00
|
|
|
if !has_doc {
|
2016-10-08 23:08:07 -05:00
|
|
|
cx.span_lint(MISSING_DOCS,
|
|
|
|
sp,
|
2015-02-28 06:31:14 -06:00
|
|
|
&format!("missing documentation for {}", desc));
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for MissingDoc {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MISSING_DOCS)
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn enter_lint_attrs(&mut self, _: &LateContext, attrs: &[ast::Attribute]) {
|
2016-10-08 23:08:07 -05:00
|
|
|
let doc_hidden = self.doc_hidden() ||
|
|
|
|
attrs.iter().any(|attr| {
|
|
|
|
attr.check_name("doc") &&
|
|
|
|
match attr.meta_item_list() {
|
2015-02-25 05:44:44 -06:00
|
|
|
None => false,
|
2017-03-24 03:31:26 -05:00
|
|
|
Some(l) => attr::list_contains_name(&l, "hidden"),
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
self.doc_hidden_stack.push(doc_hidden);
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:56:36 -06:00
|
|
|
fn exit_lint_attrs(&mut self, _: &LateContext, _attrs: &[ast::Attribute]) {
|
2015-02-25 05:44:44 -06:00
|
|
|
self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
|
|
|
|
}
|
|
|
|
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_crate(&mut self, cx: &LateContext, krate: &hir::Crate) {
|
2015-02-28 06:31:14 -06:00
|
|
|
self.check_missing_docs_attrs(cx, None, &krate.attrs, krate.span, "crate");
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
|
2015-02-25 05:44:44 -06:00
|
|
|
let desc = match it.node {
|
2015-09-03 08:29:56 -05:00
|
|
|
hir::ItemFn(..) => "a function",
|
|
|
|
hir::ItemMod(..) => "a module",
|
|
|
|
hir::ItemEnum(..) => "an enum",
|
|
|
|
hir::ItemStruct(..) => "a struct",
|
2016-08-10 13:00:17 -05:00
|
|
|
hir::ItemUnion(..) => "a union",
|
2016-12-03 20:21:06 -06:00
|
|
|
hir::ItemTrait(.., ref trait_item_refs) => {
|
2015-03-29 22:41:54 -05:00
|
|
|
// Issue #11592, traits are always considered exported, even when private.
|
2015-09-03 08:29:56 -05:00
|
|
|
if it.vis == hir::Visibility::Inherited {
|
2015-03-29 22:41:54 -05:00
|
|
|
self.private_traits.insert(it.id);
|
2016-12-03 20:21:06 -06:00
|
|
|
for trait_item_ref in trait_item_refs {
|
|
|
|
self.private_traits.insert(trait_item_ref.id.node_id);
|
2015-03-29 22:41:54 -05:00
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
return;
|
2015-03-29 22:41:54 -05:00
|
|
|
}
|
|
|
|
"a trait"
|
2016-10-08 23:08:07 -05:00
|
|
|
}
|
2015-09-03 08:29:56 -05:00
|
|
|
hir::ItemTy(..) => "a type alias",
|
2016-11-10 08:47:00 -06:00
|
|
|
hir::ItemImpl(.., Some(ref trait_ref), _, ref impl_item_refs) => {
|
2015-03-29 22:41:54 -05:00
|
|
|
// If the trait is private, add the impl items to private_traits so they don't get
|
|
|
|
// reported for missing docs.
|
2016-11-25 05:21:19 -06:00
|
|
|
let real_trait = trait_ref.path.def.def_id();
|
2017-01-25 18:41:06 -06:00
|
|
|
if let Some(node_id) = cx.tcx.hir.as_local_node_id(real_trait) {
|
|
|
|
match cx.tcx.hir.find(node_id) {
|
2016-10-08 23:08:07 -05:00
|
|
|
Some(hir_map::NodeItem(item)) => {
|
|
|
|
if item.vis == hir::Visibility::Inherited {
|
2016-11-10 08:47:00 -06:00
|
|
|
for impl_item_ref in impl_item_refs {
|
|
|
|
self.private_traits.insert(impl_item_ref.id.node_id);
|
2016-10-08 23:08:07 -05:00
|
|
|
}
|
2015-09-04 12:52:28 -05:00
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
2015-09-04 12:52:28 -05:00
|
|
|
}
|
2015-03-29 22:41:54 -05:00
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
return;
|
|
|
|
}
|
2015-09-03 08:29:56 -05:00
|
|
|
hir::ItemConst(..) => "a constant",
|
|
|
|
hir::ItemStatic(..) => "a static",
|
2016-10-08 23:08:07 -05:00
|
|
|
_ => return,
|
2015-02-25 05:44:44 -06:00
|
|
|
};
|
2015-03-29 22:41:54 -05:00
|
|
|
|
2015-02-28 06:31:14 -06:00
|
|
|
self.check_missing_docs_attrs(cx, Some(it.id), &it.attrs, it.span, desc);
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext, trait_item: &hir::TraitItem) {
|
2016-10-08 23:08:07 -05:00
|
|
|
if self.private_traits.contains(&trait_item.id) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-29 22:41:54 -05:00
|
|
|
|
2015-03-10 05:28:44 -05:00
|
|
|
let desc = match trait_item.node {
|
2016-12-03 20:21:06 -06:00
|
|
|
hir::TraitItemKind::Const(..) => "an associated constant",
|
|
|
|
hir::TraitItemKind::Method(..) => "a trait method",
|
|
|
|
hir::TraitItemKind::Type(..) => "an associated type",
|
2015-03-10 05:28:44 -05:00
|
|
|
};
|
2015-03-29 22:41:54 -05:00
|
|
|
|
2016-10-08 23:08:07 -05:00
|
|
|
self.check_missing_docs_attrs(cx,
|
|
|
|
Some(trait_item.id),
|
2015-03-10 05:28:44 -05:00
|
|
|
&trait_item.attrs,
|
2016-10-08 23:08:07 -05:00
|
|
|
trait_item.span,
|
|
|
|
desc);
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext, impl_item: &hir::ImplItem) {
|
2015-03-10 05:28:44 -05:00
|
|
|
// If the method is an impl for a trait, don't doc.
|
2017-04-18 09:54:47 -05:00
|
|
|
if method_context(cx, impl_item.id) == MethodLateContext::TraitImpl {
|
2015-03-10 05:28:44 -05:00
|
|
|
return;
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2015-03-10 05:28:44 -05:00
|
|
|
|
|
|
|
let desc = match impl_item.node {
|
2015-11-12 08:57:51 -06:00
|
|
|
hir::ImplItemKind::Const(..) => "an associated constant",
|
|
|
|
hir::ImplItemKind::Method(..) => "a method",
|
|
|
|
hir::ImplItemKind::Type(_) => "an associated type",
|
2015-03-10 05:28:44 -05:00
|
|
|
};
|
2016-10-08 23:08:07 -05:00
|
|
|
self.check_missing_docs_attrs(cx,
|
|
|
|
Some(impl_item.id),
|
2015-03-10 05:28:44 -05:00
|
|
|
&impl_item.attrs,
|
2016-10-08 23:08:07 -05:00
|
|
|
impl_item.span,
|
|
|
|
desc);
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_struct_field(&mut self, cx: &LateContext, sf: &hir::StructField) {
|
2016-02-27 02:34:29 -06:00
|
|
|
if !sf.is_positional() {
|
2017-05-27 11:06:15 -05:00
|
|
|
self.check_missing_docs_attrs(cx,
|
|
|
|
Some(sf.id),
|
|
|
|
&sf.attrs,
|
|
|
|
sf.span,
|
|
|
|
"a struct field")
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_variant(&mut self, cx: &LateContext, v: &hir::Variant, _: &hir::Generics) {
|
2016-10-08 23:08:07 -05:00
|
|
|
self.check_missing_docs_attrs(cx,
|
|
|
|
Some(v.node.data.id()),
|
|
|
|
&v.node.attrs,
|
|
|
|
v.span,
|
|
|
|
"a variant");
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub MISSING_COPY_IMPLEMENTATIONS,
|
|
|
|
Allow,
|
|
|
|
"detects potentially-forgotten implementations of `Copy`"
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 05:44:44 -06:00
|
|
|
pub struct MissingCopyImplementations;
|
|
|
|
|
|
|
|
impl LintPass for MissingCopyImplementations {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MISSING_COPY_IMPLEMENTATIONS)
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingCopyImplementations {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_item(&mut self, cx: &LateContext, item: &hir::Item) {
|
2015-11-19 05:16:35 -06:00
|
|
|
if !cx.access_levels.is_reachable(item.id) {
|
2015-02-28 06:31:14 -06:00
|
|
|
return;
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2015-08-25 13:52:15 -05:00
|
|
|
let (def, ty) = match item.node {
|
2015-09-03 08:29:56 -05:00
|
|
|
hir::ItemStruct(_, ref ast_generics) => {
|
2015-02-25 05:44:44 -06:00
|
|
|
if ast_generics.is_parameterized() {
|
2015-02-28 06:31:14 -06:00
|
|
|
return;
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2017-04-24 07:20:46 -05:00
|
|
|
let def = cx.tcx.adt_def(cx.tcx.hir.local_def_id(item.id));
|
2016-10-24 19:23:29 -05:00
|
|
|
(def, cx.tcx.mk_adt(def, cx.tcx.intern_substs(&[])))
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2016-08-10 13:00:17 -05:00
|
|
|
hir::ItemUnion(_, ref ast_generics) => {
|
|
|
|
if ast_generics.is_parameterized() {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-24 07:20:46 -05:00
|
|
|
let def = cx.tcx.adt_def(cx.tcx.hir.local_def_id(item.id));
|
2016-10-24 19:23:29 -05:00
|
|
|
(def, cx.tcx.mk_adt(def, cx.tcx.intern_substs(&[])))
|
2016-08-10 13:00:17 -05:00
|
|
|
}
|
2015-09-03 08:29:56 -05:00
|
|
|
hir::ItemEnum(_, ref ast_generics) => {
|
2015-02-25 05:44:44 -06:00
|
|
|
if ast_generics.is_parameterized() {
|
2015-02-28 06:31:14 -06:00
|
|
|
return;
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2017-04-24 07:20:46 -05:00
|
|
|
let def = cx.tcx.adt_def(cx.tcx.hir.local_def_id(item.id));
|
2016-10-24 19:23:29 -05:00
|
|
|
(def, cx.tcx.mk_adt(def, cx.tcx.intern_substs(&[])))
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
2017-02-19 06:46:29 -06:00
|
|
|
if def.has_dtor(cx.tcx) {
|
2016-10-08 23:08:07 -05:00
|
|
|
return;
|
|
|
|
}
|
2017-05-17 07:01:04 -05:00
|
|
|
let param_env = ty::ParamEnv::empty(Reveal::UserFacing);
|
2017-05-10 09:28:06 -05:00
|
|
|
if !ty.moves_by_default(cx.tcx, param_env, item.span) {
|
2015-02-28 06:31:14 -06:00
|
|
|
return;
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2017-05-10 09:28:06 -05:00
|
|
|
if param_env.can_type_implement_copy(cx.tcx, ty, item.span).is_ok() {
|
2015-02-25 05:44:44 -06:00
|
|
|
cx.span_lint(MISSING_COPY_IMPLEMENTATIONS,
|
|
|
|
item.span,
|
|
|
|
"type could implement `Copy`; consider adding `impl \
|
|
|
|
Copy`")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
MISSING_DEBUG_IMPLEMENTATIONS,
|
|
|
|
Allow,
|
|
|
|
"detects missing implementations of fmt::Debug"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MissingDebugImplementations {
|
|
|
|
impling_types: Option<NodeSet>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MissingDebugImplementations {
|
|
|
|
pub fn new() -> MissingDebugImplementations {
|
2016-10-08 23:08:07 -05:00
|
|
|
MissingDebugImplementations { impling_types: None }
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for MissingDebugImplementations {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MISSING_DEBUG_IMPLEMENTATIONS)
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_item(&mut self, cx: &LateContext, item: &hir::Item) {
|
2015-11-19 05:16:35 -06:00
|
|
|
if !cx.access_levels.is_reachable(item.id) {
|
2015-02-25 05:44:44 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match item.node {
|
2016-10-08 23:08:07 -05:00
|
|
|
hir::ItemStruct(..) |
|
|
|
|
hir::ItemUnion(..) |
|
|
|
|
hir::ItemEnum(..) => {}
|
2015-02-25 05:44:44 -06:00
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
|
|
|
|
let debug = match cx.tcx.lang_items.debug_trait() {
|
|
|
|
Some(debug) => debug,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.impling_types.is_none() {
|
2017-04-24 07:20:46 -05:00
|
|
|
let debug_def = cx.tcx.trait_def(debug);
|
2015-04-21 11:00:12 -05:00
|
|
|
let mut impls = NodeSet();
|
|
|
|
debug_def.for_each_impl(cx.tcx, |d| {
|
2017-04-24 07:20:46 -05:00
|
|
|
if let Some(ty_def) = cx.tcx.type_of(d).ty_to_def_id() {
|
2017-01-25 18:41:06 -06:00
|
|
|
if let Some(node_id) = cx.tcx.hir.as_local_node_id(ty_def) {
|
2016-11-10 08:49:53 -06:00
|
|
|
impls.insert(node_id);
|
2015-04-21 11:00:12 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2015-04-21 11:00:12 -05:00
|
|
|
});
|
|
|
|
|
2015-02-25 05:44:44 -06:00
|
|
|
self.impling_types = Some(impls);
|
|
|
|
debug!("{:?}", self.impling_types);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.impling_types.as_ref().unwrap().contains(&item.id) {
|
|
|
|
cx.span_lint(MISSING_DEBUG_IMPLEMENTATIONS,
|
|
|
|
item.span,
|
|
|
|
"type does not implement `fmt::Debug`; consider adding #[derive(Debug)] \
|
|
|
|
or a manual implementation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 21:38:46 -05:00
|
|
|
declare_lint! {
|
|
|
|
pub ANONYMOUS_PARAMETERS,
|
|
|
|
Allow,
|
|
|
|
"detects anonymous parameters"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks for use of anonymous parameters (RFC 1685)
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AnonymousParameters;
|
|
|
|
|
|
|
|
impl LintPass for AnonymousParameters {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(ANONYMOUS_PARAMETERS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for AnonymousParameters {
|
|
|
|
fn check_trait_item(&mut self, cx: &EarlyContext, it: &ast::TraitItem) {
|
|
|
|
match it.node {
|
|
|
|
ast::TraitItemKind::Method(ref sig, _) => {
|
|
|
|
for arg in sig.decl.inputs.iter() {
|
|
|
|
match arg.pat.node {
|
|
|
|
ast::PatKind::Ident(_, ident, None) => {
|
|
|
|
if ident.node.name == keywords::Invalid.name() {
|
|
|
|
cx.span_lint(ANONYMOUS_PARAMETERS,
|
|
|
|
arg.pat.span,
|
|
|
|
"use of deprecated anonymous parameter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:27 -05:00
|
|
|
declare_lint! {
|
|
|
|
DEPRECATED_ATTR,
|
|
|
|
Warn,
|
|
|
|
"detects use of deprecated attributes"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks for use of attributes which have been deprecated.
|
|
|
|
#[derive(Clone)]
|
2016-10-18 00:04:28 -05:00
|
|
|
pub struct DeprecatedAttr {
|
|
|
|
// This is not free to compute, so we want to keep it around, rather than
|
|
|
|
// compute it for every attribute.
|
|
|
|
depr_attrs: Vec<&'static (&'static str, AttributeType, AttributeGate)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeprecatedAttr {
|
|
|
|
pub fn new() -> DeprecatedAttr {
|
|
|
|
DeprecatedAttr {
|
|
|
|
depr_attrs: deprecated_attributes(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-10 21:51:27 -05:00
|
|
|
|
|
|
|
impl LintPass for DeprecatedAttr {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(DEPRECATED_ATTR)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for DeprecatedAttr {
|
|
|
|
fn check_attribute(&mut self, cx: &EarlyContext, attr: &ast::Attribute) {
|
2017-03-03 03:23:59 -06:00
|
|
|
let name = unwrap_or!(attr.name(), return);
|
2016-10-18 00:04:28 -05:00
|
|
|
for &&(n, _, ref g) in &self.depr_attrs {
|
2016-11-14 22:34:52 -06:00
|
|
|
if name == n {
|
2016-10-18 00:04:28 -05:00
|
|
|
if let &AttributeGate::Gated(Stability::Deprecated(link),
|
|
|
|
ref name,
|
|
|
|
ref reason,
|
|
|
|
_) = g {
|
2016-10-10 21:51:27 -05:00
|
|
|
cx.span_lint(DEPRECATED,
|
|
|
|
attr.span,
|
2016-10-18 00:04:28 -05:00
|
|
|
&format!("use of deprecated attribute `{}`: {}. See {}",
|
|
|
|
name, reason, link));
|
2016-10-10 21:51:27 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 02:15:07 -05:00
|
|
|
declare_lint! {
|
|
|
|
pub ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
|
|
|
|
Warn,
|
|
|
|
"floating-point literals cannot be used in patterns"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks for floating point literals in patterns.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct IllegalFloatLiteralPattern;
|
|
|
|
|
|
|
|
impl LintPass for IllegalFloatLiteralPattern {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(ILLEGAL_FLOATING_POINT_LITERAL_PATTERN)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fl_lit_check_expr(cx: &EarlyContext, expr: &ast::Expr) {
|
|
|
|
use self::ast::{ExprKind, LitKind};
|
|
|
|
match expr.node {
|
|
|
|
ExprKind::Lit(ref l) => {
|
|
|
|
match l.node {
|
|
|
|
LitKind::FloatUnsuffixed(..) |
|
|
|
|
LitKind::Float(..) => {
|
|
|
|
cx.span_lint(ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
|
|
|
|
l.span,
|
|
|
|
"floating-point literals cannot be used in patterns");
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// These may occur in patterns
|
|
|
|
// and can maybe contain float literals
|
|
|
|
ExprKind::Unary(_, ref f) => fl_lit_check_expr(cx, f),
|
2017-06-24 13:26:04 -05:00
|
|
|
// Other kinds of exprs can't occur in patterns so we don't have to check them
|
|
|
|
// (ast_validation will emit an error if they occur)
|
|
|
|
_ => (),
|
2017-04-29 02:15:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for IllegalFloatLiteralPattern {
|
|
|
|
fn check_pat(&mut self, cx: &EarlyContext, pat: &ast::Pat) {
|
|
|
|
use self::ast::PatKind;
|
|
|
|
pat.walk(&mut |p| {
|
|
|
|
match p.node {
|
|
|
|
// Wildcard patterns and paths are uninteresting for the lint
|
|
|
|
PatKind::Wild |
|
|
|
|
PatKind::Path(..) => (),
|
|
|
|
|
|
|
|
// The walk logic recurses inside these
|
|
|
|
PatKind::Ident(..) |
|
|
|
|
PatKind::Struct(..) |
|
|
|
|
PatKind::Tuple(..) |
|
|
|
|
PatKind::TupleStruct(..) |
|
|
|
|
PatKind::Ref(..) |
|
|
|
|
PatKind::Box(..) |
|
|
|
|
PatKind::Slice(..) => (),
|
|
|
|
|
|
|
|
// Extract the expressions and check them
|
|
|
|
PatKind::Lit(ref e) => fl_lit_check_expr(cx, e),
|
|
|
|
PatKind::Range(ref st, ref en, _) => {
|
|
|
|
fl_lit_check_expr(cx, st);
|
|
|
|
fl_lit_check_expr(cx, en);
|
|
|
|
},
|
|
|
|
|
|
|
|
PatKind::Mac(_) => bug!("lint must run post-expansion"),
|
|
|
|
}
|
|
|
|
true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 17:17:35 -05:00
|
|
|
declare_lint! {
|
|
|
|
pub UNUSED_DOC_COMMENT,
|
|
|
|
Warn,
|
|
|
|
"detects doc comments that aren't used by rustdoc"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct UnusedDocComment;
|
|
|
|
|
|
|
|
impl LintPass for UnusedDocComment {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array![UNUSED_DOC_COMMENT]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UnusedDocComment {
|
|
|
|
fn warn_if_doc<'a, 'tcx,
|
|
|
|
I: Iterator<Item=&'a ast::Attribute>,
|
|
|
|
C: LintContext<'tcx>>(&self, mut attrs: I, cx: &C) {
|
|
|
|
if let Some(attr) = attrs.find(|a| a.is_value_str() && a.check_name("doc")) {
|
|
|
|
cx.struct_span_lint(UNUSED_DOC_COMMENT, attr.span, "doc comment not used by rustdoc")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for UnusedDocComment {
|
|
|
|
fn check_local(&mut self, cx: &EarlyContext, decl: &ast::Local) {
|
|
|
|
self.warn_if_doc(decl.attrs.iter(), cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_arm(&mut self, cx: &EarlyContext, arm: &ast::Arm) {
|
|
|
|
self.warn_if_doc(arm.attrs.iter(), cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr) {
|
|
|
|
self.warn_if_doc(expr.attrs.iter(), cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 05:44:44 -06:00
|
|
|
declare_lint! {
|
|
|
|
pub UNCONDITIONAL_RECURSION,
|
|
|
|
Warn,
|
|
|
|
"functions that cannot return without calling themselves"
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 05:44:44 -06:00
|
|
|
pub struct UnconditionalRecursion;
|
|
|
|
|
|
|
|
|
|
|
|
impl LintPass for UnconditionalRecursion {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array![UNCONDITIONAL_RECURSION]
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnconditionalRecursion {
|
2016-10-08 23:08:07 -05:00
|
|
|
fn check_fn(&mut self,
|
|
|
|
cx: &LateContext,
|
|
|
|
fn_kind: FnKind,
|
|
|
|
_: &hir::FnDecl,
|
2016-12-21 04:32:59 -06:00
|
|
|
body: &hir::Body,
|
2016-10-08 23:08:07 -05:00
|
|
|
sp: Span,
|
|
|
|
id: ast::NodeId) {
|
2015-07-02 05:33:01 -05:00
|
|
|
let method = match fn_kind {
|
2015-08-26 05:00:14 -05:00
|
|
|
FnKind::ItemFn(..) => None,
|
|
|
|
FnKind::Method(..) => {
|
2017-01-25 18:41:06 -06:00
|
|
|
Some(cx.tcx.associated_item(cx.tcx.hir.local_def_id(id)))
|
2015-07-02 05:33:01 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
// closures can't recur, so they don't matter.
|
2016-10-08 23:08:07 -05:00
|
|
|
FnKind::Closure(_) => return,
|
2015-02-25 05:44:44 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Walk through this function (say `f`) looking to see if
|
|
|
|
// every possible path references itself, i.e. the function is
|
|
|
|
// called recursively unconditionally. This is done by trying
|
|
|
|
// to find a path from the entry node to the exit node that
|
|
|
|
// *doesn't* call `f` by traversing from the entry while
|
|
|
|
// pretending that calls of `f` are sinks (i.e. ignoring any
|
|
|
|
// exit edges from them).
|
|
|
|
//
|
|
|
|
// NB. this has an edge case with non-returning statements,
|
|
|
|
// like `loop {}` or `panic!()`: control flow never reaches
|
|
|
|
// the exit node through these, so one can have a function
|
|
|
|
// that never actually calls itselfs but is still picked up by
|
|
|
|
// this lint:
|
|
|
|
//
|
|
|
|
// fn f(cond: bool) {
|
|
|
|
// if !cond { panic!() } // could come from `assert!(cond)`
|
|
|
|
// f(false)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// In general, functions of that form may be able to call
|
|
|
|
// itself a finite number of times and then diverge. The lint
|
|
|
|
// considers this to be an error for two reasons, (a) it is
|
|
|
|
// easier to implement, and (b) it seems rare to actually want
|
|
|
|
// to have behaviour like the above, rather than
|
|
|
|
// e.g. accidentally recurring after an assert.
|
|
|
|
|
2017-02-18 05:52:16 -06:00
|
|
|
let cfg = cfg::CFG::new(cx.tcx, &body);
|
2015-02-25 05:44:44 -06:00
|
|
|
|
|
|
|
let mut work_queue = vec![cfg.entry];
|
|
|
|
let mut reached_exit_without_self_call = false;
|
|
|
|
let mut self_call_spans = vec![];
|
2015-08-11 19:27:05 -05:00
|
|
|
let mut visited = HashSet::new();
|
2015-02-25 05:44:44 -06:00
|
|
|
|
|
|
|
while let Some(idx) = work_queue.pop() {
|
|
|
|
if idx == cfg.exit {
|
|
|
|
// found a path!
|
|
|
|
reached_exit_without_self_call = true;
|
2015-02-28 06:31:14 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cfg_id = idx.node_id();
|
|
|
|
if visited.contains(&cfg_id) {
|
2015-02-25 05:44:44 -06:00
|
|
|
// already done
|
2015-02-28 06:31:14 -06:00
|
|
|
continue;
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
visited.insert(cfg_id);
|
2015-02-28 06:31:14 -06:00
|
|
|
|
2015-02-25 05:44:44 -06:00
|
|
|
let node_id = cfg.graph.node_data(idx).id();
|
|
|
|
|
|
|
|
// is this a recursive call?
|
2015-07-02 05:33:01 -05:00
|
|
|
let self_recursive = if node_id != ast::DUMMY_NODE_ID {
|
|
|
|
match method {
|
2017-01-06 13:54:24 -06:00
|
|
|
Some(ref method) => expr_refers_to_this_method(cx, method, node_id),
|
|
|
|
None => expr_refers_to_this_fn(cx, id, node_id),
|
2015-07-02 05:33:01 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if self_recursive {
|
2017-01-25 18:41:06 -06:00
|
|
|
self_call_spans.push(cx.tcx.hir.span(node_id));
|
2015-02-25 05:44:44 -06:00
|
|
|
// this is a self call, so we shouldn't explore past
|
|
|
|
// this node in the CFG.
|
2015-02-28 06:31:14 -06:00
|
|
|
continue;
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
// add the successors of this node to explore the graph further.
|
2015-04-07 05:12:13 -05:00
|
|
|
for (_, edge) in cfg.graph.outgoing_edges(idx) {
|
2015-02-25 05:44:44 -06:00
|
|
|
let target_idx = edge.target();
|
|
|
|
let target_cfg_id = target_idx.node_id();
|
|
|
|
if !visited.contains(&target_cfg_id) {
|
|
|
|
work_queue.push(target_idx)
|
|
|
|
}
|
2015-04-07 05:12:13 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
|
2015-02-28 06:31:14 -06:00
|
|
|
// Check the number of self calls because a function that
|
2015-02-25 05:44:44 -06:00
|
|
|
// doesn't return (e.g. calls a `-> !` function or `loop { /*
|
|
|
|
// no break */ }`) shouldn't be linted unless it actually
|
|
|
|
// recurs.
|
2015-03-24 18:54:09 -05:00
|
|
|
if !reached_exit_without_self_call && !self_call_spans.is_empty() {
|
2016-10-08 23:08:07 -05:00
|
|
|
let mut db = cx.struct_span_lint(UNCONDITIONAL_RECURSION,
|
|
|
|
sp,
|
2015-12-20 15:00:43 -06:00
|
|
|
"function cannot return without recurring");
|
2015-02-25 05:44:44 -06:00
|
|
|
|
|
|
|
// FIXME #19668: these could be span_lint_note's instead of this manual guard.
|
|
|
|
if cx.current_level(UNCONDITIONAL_RECURSION) != Level::Allow {
|
|
|
|
// offer some help to the programmer.
|
|
|
|
for call in &self_call_spans {
|
2015-12-23 00:27:20 -06:00
|
|
|
db.span_note(*call, "recursive call site");
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2016-04-20 13:49:16 -05:00
|
|
|
db.help("a `loop` may express intention \
|
|
|
|
better if this is on purpose");
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2015-12-23 00:27:20 -06:00
|
|
|
db.emit();
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// all done
|
|
|
|
return;
|
|
|
|
|
2015-07-02 05:33:01 -05:00
|
|
|
// Functions for identifying if the given Expr NodeId `id`
|
|
|
|
// represents a call to the function `fn_id`/method `method`.
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2017-01-06 13:54:24 -06:00
|
|
|
fn expr_refers_to_this_fn(cx: &LateContext, fn_id: ast::NodeId, id: ast::NodeId) -> bool {
|
2017-01-25 18:41:06 -06:00
|
|
|
match cx.tcx.hir.get(id) {
|
2015-07-31 02:04:06 -05:00
|
|
|
hir_map::NodeExpr(&hir::Expr { node: hir::ExprCall(ref callee, _), .. }) => {
|
2016-11-25 05:21:19 -06:00
|
|
|
let def = if let hir::ExprPath(ref qpath) = callee.node {
|
2017-01-06 13:54:24 -06:00
|
|
|
cx.tables.qpath_def(qpath, callee.id)
|
2016-11-25 05:21:19 -06:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
};
|
2017-01-25 18:41:06 -06:00
|
|
|
def.def_id() == cx.tcx.hir.local_def_id(fn_id)
|
2015-06-29 16:51:56 -05:00
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
_ => false,
|
2015-06-29 16:51:56 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
|
2015-08-03 17:17:56 -05:00
|
|
|
// Check if the expression `id` performs a call to `method`.
|
2017-01-06 13:54:24 -06:00
|
|
|
fn expr_refers_to_this_method(cx: &LateContext,
|
|
|
|
method: &ty::AssociatedItem,
|
|
|
|
id: ast::NodeId)
|
|
|
|
-> bool {
|
2016-10-19 22:33:20 -05:00
|
|
|
use rustc::ty::adjustment::*;
|
|
|
|
|
2017-05-20 08:11:07 -05:00
|
|
|
// Ignore non-expressions.
|
|
|
|
let expr = if let hir_map::NodeExpr(e) = cx.tcx.hir.get(id) {
|
|
|
|
e
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
};
|
2015-08-03 17:17:56 -05:00
|
|
|
|
|
|
|
// Check for overloaded autoderef method calls.
|
2017-05-27 02:29:24 -05:00
|
|
|
let mut source = cx.tables.expr_ty(expr);
|
|
|
|
for adjustment in cx.tables.expr_adjustments(expr) {
|
|
|
|
if let Adjust::Deref(Some(deref)) = adjustment.kind {
|
|
|
|
let (def_id, substs) = deref.method_call(cx.tcx, source);
|
2017-06-07 07:21:55 -05:00
|
|
|
if method_call_refers_to_method(cx, method, def_id, substs, id) {
|
2017-05-27 02:29:24 -05:00
|
|
|
return true;
|
2015-08-03 17:17:56 -05:00
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:29:24 -05:00
|
|
|
source = adjustment.target;
|
2015-08-03 17:17:56 -05:00
|
|
|
}
|
|
|
|
|
2017-05-20 08:11:07 -05:00
|
|
|
// Check for method calls and overloaded operators.
|
|
|
|
if cx.tables.is_method_call(expr) {
|
|
|
|
let def_id = cx.tables.type_dependent_defs[&id].def_id();
|
|
|
|
let substs = cx.tables.node_substs(id);
|
2017-06-07 07:21:55 -05:00
|
|
|
if method_call_refers_to_method(cx, method, def_id, substs, id) {
|
2017-05-20 08:11:07 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 17:17:56 -05:00
|
|
|
// Check for calls to methods via explicit paths (e.g. `T::method()`).
|
2017-05-20 08:11:07 -05:00
|
|
|
match expr.node {
|
|
|
|
hir::ExprCall(ref callee, _) => {
|
2016-11-25 05:21:19 -06:00
|
|
|
let def = if let hir::ExprPath(ref qpath) = callee.node {
|
2017-01-06 13:54:24 -06:00
|
|
|
cx.tables.qpath_def(qpath, callee.id)
|
2016-11-25 05:21:19 -06:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
match def {
|
|
|
|
Def::Method(def_id) => {
|
2017-05-20 02:26:08 -05:00
|
|
|
let substs = cx.tables.node_substs(callee.id);
|
2017-06-07 07:21:55 -05:00
|
|
|
method_call_refers_to_method(cx, method, def_id, substs, id)
|
2015-08-03 17:17:56 -05:00
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
_ => false,
|
2015-08-03 17:17:56 -05:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
_ => false,
|
2015-08-03 17:17:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the method call to the method with the ID `callee_id`
|
|
|
|
// and instantiated with `callee_substs` refers to method `method`.
|
2017-06-07 07:21:55 -05:00
|
|
|
fn method_call_refers_to_method<'a, 'tcx>(cx: &LateContext<'a, 'tcx>,
|
2016-11-09 18:06:34 -06:00
|
|
|
method: &ty::AssociatedItem,
|
2016-05-02 20:56:42 -05:00
|
|
|
callee_id: DefId,
|
|
|
|
callee_substs: &Substs<'tcx>,
|
2016-10-08 23:08:07 -05:00
|
|
|
expr_id: ast::NodeId)
|
|
|
|
-> bool {
|
2017-06-07 07:21:55 -05:00
|
|
|
let tcx = cx.tcx;
|
2016-11-09 18:06:34 -06:00
|
|
|
let callee_item = tcx.associated_item(callee_id);
|
2015-07-02 05:33:01 -05:00
|
|
|
|
2016-11-09 18:06:34 -06:00
|
|
|
match callee_item.container {
|
2015-07-02 05:33:01 -05:00
|
|
|
// This is an inherent method, so the `def_id` refers
|
|
|
|
// directly to the method definition.
|
2016-10-08 23:08:07 -05:00
|
|
|
ty::ImplContainer(_) => callee_id == method.def_id,
|
2015-07-02 05:33:01 -05:00
|
|
|
|
|
|
|
// A trait method, from any number of possible sources.
|
|
|
|
// Attempt to select a concrete impl before checking.
|
|
|
|
ty::TraitContainer(trait_def_id) => {
|
2016-08-08 15:39:49 -05:00
|
|
|
let trait_ref = ty::TraitRef::from_method(tcx, trait_def_id, callee_substs);
|
2015-07-02 05:33:01 -05:00
|
|
|
let trait_ref = ty::Binder(trait_ref);
|
2017-01-25 18:41:06 -06:00
|
|
|
let span = tcx.hir.span(expr_id);
|
2015-07-02 05:33:01 -05:00
|
|
|
let obligation =
|
2015-08-03 17:17:56 -05:00
|
|
|
traits::Obligation::new(traits::ObligationCause::misc(span, expr_id),
|
2017-06-07 07:21:55 -05:00
|
|
|
cx.param_env,
|
2015-07-02 05:33:01 -05:00
|
|
|
trait_ref.to_poly_trait_predicate());
|
|
|
|
|
2017-06-09 02:55:16 -05:00
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2016-03-24 22:22:44 -05:00
|
|
|
let mut selcx = traits::SelectionContext::new(&infcx);
|
|
|
|
match selcx.select(&obligation) {
|
|
|
|
// The method comes from a `T: Trait` bound.
|
|
|
|
// If `T` is `Self`, then this call is inside
|
|
|
|
// a default method definition.
|
|
|
|
Ok(Some(traits::VtableParam(_))) => {
|
2016-08-08 15:39:49 -05:00
|
|
|
let on_self = trait_ref.self_ty().is_self();
|
2016-03-24 22:22:44 -05:00
|
|
|
// We can only be recurring in a default
|
|
|
|
// method if we're being called literally
|
|
|
|
// on the `Self` type.
|
|
|
|
on_self && callee_id == method.def_id
|
|
|
|
}
|
2015-07-02 00:52:36 -05:00
|
|
|
|
2016-03-24 22:22:44 -05:00
|
|
|
// The `impl` is known, so we check that with a
|
|
|
|
// special case:
|
|
|
|
Ok(Some(traits::VtableImpl(vtable_impl))) => {
|
|
|
|
let container = ty::ImplContainer(vtable_impl.impl_def_id);
|
|
|
|
// It matches if it comes from the same impl,
|
|
|
|
// and has the same method name.
|
2016-11-09 18:06:34 -06:00
|
|
|
container == method.container && callee_item.name == method.name
|
2016-03-24 22:22:44 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-03-24 22:22:44 -05:00
|
|
|
// There's no way to know if this call is
|
|
|
|
// recursive, so we assume it's not.
|
2016-10-08 23:08:07 -05:00
|
|
|
_ => false,
|
2016-03-24 22:22:44 -05:00
|
|
|
}
|
|
|
|
})
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2015-07-02 05:33:01 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
PLUGIN_AS_LIBRARY,
|
|
|
|
Warn,
|
|
|
|
"compiler plugin used as ordinary library in non-plugin crate"
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 05:44:44 -06:00
|
|
|
pub struct PluginAsLibrary;
|
|
|
|
|
|
|
|
impl LintPass for PluginAsLibrary {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array![PLUGIN_AS_LIBRARY]
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PluginAsLibrary {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
|
2015-02-25 05:44:44 -06:00
|
|
|
if cx.sess().plugin_registrar_fn.get().is_some() {
|
|
|
|
// We're compiling a plugin; it's fine to link other plugins.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match it.node {
|
2015-09-03 08:29:56 -05:00
|
|
|
hir::ItemExternCrate(..) => (),
|
2015-02-25 05:44:44 -06:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2015-11-21 13:39:05 -06:00
|
|
|
let prfn = match cx.sess().cstore.extern_mod_stmt_cnum(it.id) {
|
2015-11-20 09:46:39 -06:00
|
|
|
Some(cnum) => cx.sess().cstore.plugin_registrar_fn(cnum),
|
2015-02-25 05:44:44 -06:00
|
|
|
None => {
|
|
|
|
// Probably means we aren't linking the crate for some reason.
|
|
|
|
//
|
|
|
|
// Not sure if / when this could happen.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-20 09:46:39 -06:00
|
|
|
if prfn.is_some() {
|
2016-10-08 23:08:07 -05:00
|
|
|
cx.span_lint(PLUGIN_AS_LIBRARY,
|
|
|
|
it.span,
|
2015-02-28 06:31:14 -06:00
|
|
|
"compiler plugin used as an ordinary library");
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
PRIVATE_NO_MANGLE_FNS,
|
|
|
|
Warn,
|
|
|
|
"functions marked #[no_mangle] should be exported"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
PRIVATE_NO_MANGLE_STATICS,
|
|
|
|
Warn,
|
|
|
|
"statics marked #[no_mangle] should be exported"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
NO_MANGLE_CONST_ITEMS,
|
|
|
|
Deny,
|
|
|
|
"const items will not have their symbols exported"
|
|
|
|
}
|
|
|
|
|
2015-12-08 10:48:40 -06:00
|
|
|
declare_lint! {
|
|
|
|
NO_MANGLE_GENERIC_ITEMS,
|
|
|
|
Warn,
|
|
|
|
"generic items must be mangled"
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 05:44:44 -06:00
|
|
|
pub struct InvalidNoMangleItems;
|
|
|
|
|
|
|
|
impl LintPass for InvalidNoMangleItems {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(PRIVATE_NO_MANGLE_FNS,
|
|
|
|
PRIVATE_NO_MANGLE_STATICS,
|
2015-12-08 10:48:40 -06:00
|
|
|
NO_MANGLE_CONST_ITEMS,
|
|
|
|
NO_MANGLE_GENERIC_ITEMS)
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
|
2015-02-25 05:44:44 -06:00
|
|
|
match it.node {
|
2016-08-26 11:23:42 -05:00
|
|
|
hir::ItemFn(.., ref generics, _) => {
|
2017-06-03 16:54:08 -05:00
|
|
|
if attr::contains_name(&it.attrs, "no_mangle") &&
|
|
|
|
!attr::contains_name(&it.attrs, "linkage") {
|
2015-12-08 10:48:40 -06:00
|
|
|
if !cx.access_levels.is_reachable(it.id) {
|
|
|
|
let msg = format!("function {} is marked #[no_mangle], but not exported",
|
|
|
|
it.name);
|
|
|
|
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, &msg);
|
|
|
|
}
|
2017-05-28 09:12:19 -05:00
|
|
|
if generics.is_type_parameterized() {
|
2015-12-08 10:48:40 -06:00
|
|
|
cx.span_lint(NO_MANGLE_GENERIC_ITEMS,
|
|
|
|
it.span,
|
2017-05-28 09:12:19 -05:00
|
|
|
"functions generic over types must be mangled");
|
2015-12-08 10:48:40 -06:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
}
|
2015-09-03 08:29:56 -05:00
|
|
|
hir::ItemStatic(..) => {
|
2015-03-18 11:14:54 -05:00
|
|
|
if attr::contains_name(&it.attrs, "no_mangle") &&
|
2016-10-08 23:08:07 -05:00
|
|
|
!cx.access_levels.is_reachable(it.id) {
|
2015-02-25 05:44:44 -06:00
|
|
|
let msg = format!("static {} is marked #[no_mangle], but not exported",
|
2015-09-19 20:50:30 -05:00
|
|
|
it.name);
|
2015-03-07 20:08:48 -06:00
|
|
|
cx.span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, &msg);
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
}
|
2015-09-03 08:29:56 -05:00
|
|
|
hir::ItemConst(..) => {
|
2015-03-18 11:14:54 -05:00
|
|
|
if attr::contains_name(&it.attrs, "no_mangle") {
|
2015-02-25 05:44:44 -06:00
|
|
|
// Const items do not refer to a particular location in memory, and therefore
|
|
|
|
// don't have anything to attach a symbol to
|
|
|
|
let msg = "const items should never be #[no_mangle], consider instead using \
|
2015-02-28 06:31:14 -06:00
|
|
|
`pub static`";
|
2015-02-25 05:44:44 -06:00
|
|
|
cx.span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
_ => {}
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 16:49:10 -05:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct MutableTransmutes;
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
MUTABLE_TRANSMUTES,
|
|
|
|
Deny,
|
|
|
|
"mutating transmuted &mut T from &T may cause undefined behavior"
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for MutableTransmutes {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MUTABLE_TRANSMUTES)
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-04-13 16:49:10 -05:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableTransmutes {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_expr(&mut self, cx: &LateContext, expr: &hir::Expr) {
|
2016-02-05 06:13:36 -06:00
|
|
|
use syntax::abi::Abi::RustIntrinsic;
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2016-10-31 13:41:22 -05:00
|
|
|
let msg = "mutating transmuted &mut T from &T may cause undefined behavior, \
|
2015-04-13 16:49:10 -05:00
|
|
|
consider instead using an UnsafeCell";
|
|
|
|
match get_transmute_from_to(cx, expr) {
|
2015-06-11 18:21:46 -05:00
|
|
|
Some((&ty::TyRef(_, from_mt), &ty::TyRef(_, to_mt))) => {
|
2016-10-08 23:08:07 -05:00
|
|
|
if to_mt.mutbl == hir::Mutability::MutMutable &&
|
|
|
|
from_mt.mutbl == hir::Mutability::MutImmutable {
|
2015-04-13 16:49:10 -05:00
|
|
|
cx.span_lint(MUTABLE_TRANSMUTES, expr.span, msg);
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 23:08:07 -05:00
|
|
|
_ => (),
|
2015-04-13 16:49:10 -05:00
|
|
|
}
|
|
|
|
|
2016-10-08 23:08:07 -05:00
|
|
|
fn get_transmute_from_to<'a, 'tcx>
|
|
|
|
(cx: &LateContext<'a, 'tcx>,
|
|
|
|
expr: &hir::Expr)
|
|
|
|
-> Option<(&'tcx ty::TypeVariants<'tcx>, &'tcx ty::TypeVariants<'tcx>)> {
|
2016-11-25 05:21:19 -06:00
|
|
|
let def = if let hir::ExprPath(ref qpath) = expr.node {
|
2017-01-06 13:54:24 -06:00
|
|
|
cx.tables.qpath_def(qpath, expr.id)
|
2016-11-25 05:21:19 -06:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
if let Def::Fn(did) = def {
|
2015-04-13 16:49:10 -05:00
|
|
|
if !def_id_is_transmute(cx, did) {
|
|
|
|
return None;
|
|
|
|
}
|
2017-05-13 09:11:52 -05:00
|
|
|
let sig = cx.tables.node_id_to_type(expr.id).fn_sig(cx.tcx);
|
|
|
|
let from = sig.inputs().skip_binder()[0];
|
|
|
|
let to = *sig.output().skip_binder();
|
|
|
|
return Some((&from.sty, &to.sty));
|
2015-04-13 16:49:10 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2015-09-09 23:40:59 -05:00
|
|
|
fn def_id_is_transmute(cx: &LateContext, def_id: DefId) -> bool {
|
2017-05-13 09:11:52 -05:00
|
|
|
cx.tcx.fn_sig(def_id).abi() == RustIntrinsic &&
|
2016-11-17 08:04:20 -06:00
|
|
|
cx.tcx.item_name(def_id) == "transmute"
|
2015-04-13 16:49:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 05:44:44 -06:00
|
|
|
/// Forbids using the `#[feature(...)]` attribute
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 05:44:44 -06:00
|
|
|
pub struct UnstableFeatures;
|
|
|
|
|
2015-02-28 06:31:14 -06:00
|
|
|
declare_lint! {
|
|
|
|
UNSTABLE_FEATURES,
|
|
|
|
Allow,
|
2015-06-17 19:48:16 -05:00
|
|
|
"enabling unstable features (deprecated. do not use)"
|
2015-02-28 06:31:14 -06:00
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
|
|
|
|
impl LintPass for UnstableFeatures {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNSTABLE_FEATURES)
|
|
|
|
}
|
2015-09-14 18:35:25 -05:00
|
|
|
}
|
2015-09-09 23:40:59 -05:00
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnstableFeatures {
|
2015-09-09 23:40:59 -05:00
|
|
|
fn check_attribute(&mut self, ctx: &LateContext, attr: &ast::Attribute) {
|
2017-03-03 03:23:59 -06:00
|
|
|
if attr.check_name("feature") {
|
|
|
|
if let Some(items) = attr.meta_item_list() {
|
2015-05-18 09:37:05 -05:00
|
|
|
for item in items {
|
2016-07-15 15:13:17 -05:00
|
|
|
ctx.span_lint(UNSTABLE_FEATURES, item.span(), "unstable feature");
|
2015-05-18 09:37:05 -05:00
|
|
|
}
|
|
|
|
}
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 11:20:30 -05:00
|
|
|
|
|
|
|
/// Lint for unions that contain fields with possibly non-trivial destructors.
|
|
|
|
pub struct UnionsWithDropFields;
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
UNIONS_WITH_DROP_FIELDS,
|
|
|
|
Warn,
|
|
|
|
"use of unions that contain fields with possibly non-trivial drop code"
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for UnionsWithDropFields {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNIONS_WITH_DROP_FIELDS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:14:47 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnionsWithDropFields {
|
2016-08-19 11:20:30 -05:00
|
|
|
fn check_item(&mut self, ctx: &LateContext, item: &hir::Item) {
|
|
|
|
if let hir::ItemUnion(ref vdata, _) = item.node {
|
|
|
|
for field in vdata.fields() {
|
2017-04-24 07:20:46 -05:00
|
|
|
let field_ty = ctx.tcx.type_of(ctx.tcx.hir.local_def_id(field.id));
|
2017-06-07 07:21:55 -05:00
|
|
|
if field_ty.needs_drop(ctx.tcx, ctx.param_env) {
|
2016-08-19 11:20:30 -05:00
|
|
|
ctx.span_lint(UNIONS_WITH_DROP_FIELDS,
|
|
|
|
field.span,
|
|
|
|
"union contains a field with possibly non-trivial drop code, \
|
|
|
|
drop code of union fields is ignored when dropping the union");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|