]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #179 from nweston/step-by-zero
authorllogiq <bogusandre@gmail.com>
Sun, 16 Aug 2015 18:12:52 +0000 (20:12 +0200)
committerllogiq <bogusandre@gmail.com>
Sun, 16 Aug 2015 18:12:52 +0000 (20:12 +0200)
New lint: Range::step_by(0) (fixes #95)

22 files changed:
src/approx_const.rs
src/attrs.rs
src/bit_mask.rs
src/collapsible_if.rs
src/eq_op.rs
src/eta_reduction.rs
src/identity_op.rs
src/len_zero.rs
src/lib.rs
src/lifetimes.rs
src/loops.rs
src/methods.rs
src/misc.rs
src/mut_mut.rs
src/needless_bool.rs
src/ptr_arg.rs
src/returns.rs
src/strings.rs
src/types.rs
src/unicode.rs
src/utils.rs
tests/compile-fail/toplevel_ref_arg.rs

index 3c39b79885c9a74d25d2cf71eb049e14a9e4a17a..cfd646765c9b3af72bafe137e2cd27053d52764d 100644 (file)
@@ -1,12 +1,8 @@
-use rustc::plugin::Registry;
 use rustc::lint::*;
-use rustc::middle::const_eval::lookup_const_by_id;
-use rustc::middle::def::*;
 use syntax::ast::*;
-use syntax::ast_util::{is_comparison_binop, binop_to_string};
-use syntax::ptr::P;
 use syntax::codemap::Span;
 use std::f64::consts as f64;
+
 use utils::span_lint;
 
 declare_lint! {
index ef3320d2543f61cc8d5347ab2c2b2df57faf99b1..3e451ac5edaf655cdda36836f42591b4a4ef7d6d 100644 (file)
@@ -1,11 +1,9 @@
-/// checks for attributes
+//! checks for attributes
 
-use rustc::plugin::Registry;
 use rustc::lint::*;
 use syntax::ast::*;
-use syntax::ptr::P;
-use syntax::codemap::{Span, ExpnInfo};
-use syntax::parse::token::InternedString;
+use syntax::codemap::ExpnInfo;
+
 use utils::{in_macro, match_path, span_lint};
 
 declare_lint! { pub INLINE_ALWAYS, Warn,
@@ -103,7 +101,7 @@ fn check_attrs(cx: &Context, info: Option<&ExpnInfo>, ident: &Ident,
                 span_lint(cx, INLINE_ALWAYS, attr.span, &format!(
                     "you have declared `#[inline(always)]` on `{}`. This \
                      is usually a bad idea. Are you sure?",
-                    ident.name.as_str()));
+                    ident.name));
             }
         }
     }
index 169975001b903e874a6e0c72e729e9258f4d7844..ec937dbab6cdc143415a759f84e61abe1d6c4a6b 100644 (file)
@@ -1,11 +1,10 @@
-use rustc::plugin::Registry;
 use rustc::lint::*;
 use rustc::middle::const_eval::lookup_const_by_id;
 use rustc::middle::def::*;
 use syntax::ast::*;
-use syntax::ast_util::{is_comparison_binop, binop_to_string};
-use syntax::ptr::P;
+use syntax::ast_util::is_comparison_binop;
 use syntax::codemap::Span;
+
 use utils::span_lint;
 
 declare_lint! {
index 8a41f208938b1271bc5e39ada5e9a55a8ecf9d7c..0b6dfc19e6b5bd1d789b17f29f5bdddfabbfbaad 100644 (file)
 //!
 //! This lint is **warn** by default
 
-use rustc::plugin::Registry;
 use rustc::lint::*;
-use rustc::middle::def::*;
 use syntax::ast::*;
-use syntax::ptr::P;
-use syntax::codemap::{Span, Spanned, ExpnInfo};
+use syntax::codemap::{Spanned, ExpnInfo};
+
 use utils::{in_macro, span_help_and_lint, snippet, snippet_block};
 
 declare_lint! {
index 495696b810cf60b7ac15baaecf67511095d23284..50b61e233564e4165328d83ce9b934117a777730 100644 (file)
@@ -3,6 +3,7 @@
 use syntax::ast_util as ast_util;
 use syntax::ptr::P;
 use syntax::codemap as code;
+
 use utils::span_lint;
 
 declare_lint! {
index e0d4182081f61027aebc12393a878e37be351f29..6712e7872783d3850a163c1640f499ea3ac68bc1 100644 (file)
@@ -1,6 +1,5 @@
+use rustc::lint::*;
 use syntax::ast::*;
-use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
-use syntax::codemap::{Span, Spanned};
 use syntax::print::pprust::expr_to_string;
 
 use utils::span_lint;
index 964675b765eca632715095760e58c30ee2106a2d..18a475bb7374985f91edd63e6b9480617c71bb54 100644 (file)
@@ -1,10 +1,7 @@
-use rustc::plugin::Registry;
 use rustc::lint::*;
 use rustc::middle::const_eval::lookup_const_by_id;
 use rustc::middle::def::*;
 use syntax::ast::*;
-use syntax::ast_util::{is_comparison_binop, binop_to_string};
-use syntax::ptr::P;
 use syntax::codemap::Span;
 
 use utils::{span_lint, snippet};
index d5f3d1ad8100142ea957048ca79d13abdc056a14..073dcea582d652450184a0d72f7a45cd94d354b7 100644 (file)
@@ -1,14 +1,9 @@
-extern crate rustc_typeck as typeck;
-
-use std::rc::Rc;
-use std::cell::RefCell;
+use rustc::lint::*;
+use syntax::ast::*;
 use syntax::ptr::P;
-use rustc::lint::{Context, LintPass, LintArray, Lint};
-use rustc::util::nodemap::DefIdMap;
-use rustc::middle::ty::{self, TypeVariants, TypeAndMut, MethodTraitItemId, ImplOrTraitItemId};
-use rustc::middle::def::{DefTy, DefStruct, DefTrait};
 use syntax::codemap::{Span, Spanned};
-use syntax::ast::*;
+use rustc::middle::ty::{self, MethodTraitItemId, ImplOrTraitItemId};
+
 use utils::{span_lint, walk_ptrs_ty, snippet};
 
 declare_lint!(pub LEN_ZERO, Warn,
@@ -55,7 +50,7 @@ fn is_named_self(item: &TraitItem, name: &str) -> bool {
     }
 
     if !trait_items.iter().any(|i| is_named_self(i, "is_empty")) {
-        //span_lint(cx, LEN_WITHOUT_IS_EMPTY, item.span, &format!("trait {}", item.ident.as_str()));
+        //span_lint(cx, LEN_WITHOUT_IS_EMPTY, item.span, &format!("trait {}", item.ident));
         for i in trait_items {
             if is_named_self(i, "len") {
                 span_lint(cx, LEN_WITHOUT_IS_EMPTY, i.span,
@@ -122,7 +117,7 @@ fn is_is_empty(cx: &Context, id: &ImplOrTraitItemId) -> bool {
         if let &MethodTraitItemId(def_id) = id {
             if let ty::MethodTraitItem(ref method) =
                 cx.tcx.impl_or_trait_item(def_id) {
-                    method.name.as_str() == "is_empty"
+                    method.name == "is_empty"
                         && method.fty.sig.skip_binder().inputs.len() == 1
                 } else { false }
         } else { false }
index 91859005bf3d0e2b6b61ca275e891fa62b903938..967865b2c0cd7a3602ae60f4ba84a1d4b5f044f2 100755 (executable)
@@ -1,7 +1,6 @@
 #![feature(plugin_registrar, box_syntax)]
-#![feature(rustc_private, collections)]
+#![feature(rustc_private, core, collections)]
 #![feature(str_split_at)]
-#![allow(unused_imports, unknown_lints)]
 
 #[macro_use]
 extern crate syntax;
@@ -9,6 +8,7 @@
 extern crate rustc;
 
 // Only for the compile time checking of paths
+extern crate core;
 extern crate collections;
 
 use rustc::plugin::Registry;
index 0127822dbde264f689daf7eae92a7d1f1af12579..9d07df4a3ed21a5e7822326578907c08603ba585 100644 (file)
@@ -1,10 +1,10 @@
 use syntax::ast::*;
-use rustc::lint::{Context, LintPass, LintArray, Lint};
+use rustc::lint::*;
 use syntax::codemap::Span;
-use syntax::visit::{Visitor, FnKind, walk_ty};
-use utils::{in_external_macro, span_lint};
+use syntax::visit::{Visitor, walk_ty};
 use std::collections::HashSet;
-use std::iter::FromIterator;
+
+use utils::{in_external_macro, span_lint};
 
 declare_lint!(pub NEEDLESS_LIFETIMES, Warn,
               "using explicit lifetimes for references in function arguments when elision rules \
@@ -153,7 +153,7 @@ fn unique_lifetimes(lts: &[RefLt]) -> usize {
 impl RefVisitor {
     fn record(&mut self, lifetime: &Option<Lifetime>) {
         if let &Some(ref lt) = lifetime {
-            if lt.name.as_str() == "'static" {
+            if lt.name == "'static" {
                 self.0.push(Static);
             } else {
                 self.0.push(Named(lt.name));
index 74015bdc6beddf676a7645d293aada3104d93912..092b5ce1196c42a1e1ea4390e4bb16853edfccbb 100644 (file)
@@ -36,13 +36,12 @@ fn check_expr(&mut self, cx: &Context, expr: &Expr) {
                             span_lint(cx, NEEDLESS_RANGE_LOOP, expr.span, &format!(
                                 "the loop variable `{}` is used to index `{}`. Consider using \
                                  `for ({}, item) in {}.iter().enumerate()` or similar iterators.",
-                                ident.node.name.as_str(), indexed.as_str(),
-                                ident.node.name.as_str(), indexed.as_str()));
+                                ident.node.name, indexed, ident.node.name, indexed));
                         } else {
                             span_lint(cx, NEEDLESS_RANGE_LOOP, expr.span, &format!(
                                 "the loop variable `{}` is only used to index `{}`. \
                                  Consider using `for item in &{}` or similar iterators.",
-                                ident.node.name.as_str(), indexed.as_str(), indexed.as_str()));
+                                ident.node.name, indexed, indexed));
                         }
                     }
                 }
@@ -52,7 +51,7 @@ fn check_expr(&mut self, cx: &Context, expr: &Expr) {
             if let ExprMethodCall(ref method, _, ref args) = arg.node {
                 // just the receiver, no arguments to iter() or iter_mut()
                 if args.len() == 1 {
-                    let method_name = method.node.name.as_str();
+                    let method_name = method.node.name;
                     if method_name == "iter" {
                         let object = snippet(cx, args[0].span, "_");
                         span_lint(cx, EXPLICIT_ITER_LOOP, expr.span, &format!(
index 02b181a46e7d32fe6d68668888bac3eb8f409a2b..f2df736bebc1f5388b2ce8935f850af80c403963 100644 (file)
@@ -1,5 +1,5 @@
 use syntax::ast::*;
-use rustc::lint::{Context, LintPass, LintArray};
+use rustc::lint::*;
 use rustc::middle::ty;
 
 use utils::{span_lint, match_def_path, walk_ptrs_ty};
 declare_lint!(pub STRING_TO_STRING, Warn,
               "calling `String.to_string()` which is a no-op");
 
+#[allow(unused_imports)]
 impl LintPass for MethodsPass {
     fn get_lints(&self) -> LintArray {
         lint_array!(OPTION_UNWRAP_USED, RESULT_UNWRAP_USED, STR_TO_STRING, STRING_TO_STRING)
     }
 
     fn check_expr(&mut self, cx: &Context, expr: &Expr) {
+        {
+            // In case stuff gets moved around
+            use core::option::Option;
+            use core::result::Result;
+            use collections::string::String;
+        }
         if let ExprMethodCall(ref ident, _, ref args) = expr.node {
             let ref obj_ty = walk_ptrs_ty(cx.tcx.expr_ty(&*args[0])).sty;
             if ident.node.name == "unwrap" {
index 091ea36f2f55a6515e8e681c9618f39f04cbf26a..82ea78d97e1c4cdbb1bdb75d306c4f1b5342a1d9 100644 (file)
@@ -1,11 +1,11 @@
+use rustc::lint::*;
 use syntax::ptr::P;
 use syntax::ast;
 use syntax::ast::*;
 use syntax::ast_util::{is_comparison_binop, binop_to_string};
-use syntax::visit::{FnKind};
-use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
-use rustc::middle::ty;
 use syntax::codemap::{Span, Spanned};
+use syntax::visit::FnKind;
+use rustc::middle::ty;
 use std::borrow::Cow;
 
 use utils::{match_path, snippet, snippet_block, span_lint, span_help_and_lint, walk_ptrs_ty};
@@ -81,7 +81,11 @@ fn get_lints(&self) -> LintArray {
         lint_array!(TOPLEVEL_REF_ARG)
     }
 
-    fn check_fn(&mut self, cx: &Context, _: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
+    fn check_fn(&mut self, cx: &Context, k: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
+        if let FnKind::FkFnBlock = k {
+            // Does not apply to closures
+            return
+        }
         for ref arg in &decl.inputs {
             if let PatIdent(BindByRef(_), _, _) = arg.pat.node {
                 span_lint(cx,
index a3c40d06f90d15c7052adeee19e9a7487d480896..fbcb70e17d39196b667c4aab9282f56055ba1934 100644 (file)
@@ -1,8 +1,8 @@
-use syntax::ptr::P;
+use rustc::lint::*;
 use syntax::ast::*;
-use rustc::lint::{Context, LintPass, LintArray, Lint};
-use rustc::middle::ty::{TypeVariants, TypeAndMut, TyRef};
-use syntax::codemap::{BytePos, ExpnInfo, Span};
+use syntax::codemap::ExpnInfo;
+use rustc::middle::ty::{TypeAndMut, TyRef};
+
 use utils::{in_macro, span_lint};
 
 declare_lint!(pub MUT_MUT, Warn,
index 2a4ed50b93d32daa6dc48ce5534ee9a8e16db169..18d98f1f0630a0ca2aaeb05d959bb3d21fedfdee 100644 (file)
@@ -2,14 +2,9 @@
 //!
 //! This lint is **warn** by default
 
-use rustc::plugin::Registry;
 use rustc::lint::*;
-use rustc::middle::const_eval::lookup_const_by_id;
-use rustc::middle::def::*;
 use syntax::ast::*;
-use syntax::ast_util::{is_comparison_binop, binop_to_string};
-use syntax::ptr::P;
-use syntax::codemap::Span;
+
 use utils::{de_p, span_lint, snippet};
 
 declare_lint! {
index 85db4aa7b21dba44024fae36bf95cdb7d5d5063e..2748d187a4ed88b8d91b789ec2e9115f426b4cff 100644 (file)
@@ -2,14 +2,10 @@
 //!
 //! This lint is **warn** by default
 
-use rustc::plugin::Registry;
 use rustc::lint::*;
-use rustc::middle::const_eval::lookup_const_by_id;
-use rustc::middle::def::*;
 use syntax::ast::*;
-use syntax::ast_util::{is_comparison_binop, binop_to_string};
-use syntax::ptr::P;
 use syntax::codemap::Span;
+
 use types::match_ty_unwrap;
 use utils::span_lint;
 
index 94b9ec9650f7b1dfdd7e58370466e3dc68ba837b..df0b93f301eae573a2923823a4d8730037229a2d 100644 (file)
@@ -1,8 +1,7 @@
-use syntax::ast;
+use rustc::lint::*;
 use syntax::ast::*;
 use syntax::codemap::{Span, Spanned};
 use syntax::visit::FnKind;
-use rustc::lint::{Context, LintPass, LintArray, Level};
 
 use utils::{span_lint, snippet, match_path};
 
@@ -101,7 +100,7 @@ fn get_lints(&self) -> LintArray {
     }
 
     fn check_fn(&mut self, cx: &Context, _: FnKind, _: &FnDecl,
-                block: &Block, _: Span, _: ast::NodeId) {
+                block: &Block, _: Span, _: NodeId) {
         self.check_block_return(cx, block);
         self.check_let_return(cx, block);
     }
index 7b7bab49b5d7a0d9b70e9de1a9336acf48088738..7981b7858501a295c9d685ccc7e341532cd479f8 100644 (file)
@@ -6,9 +6,9 @@
 use rustc::lint::*;
 use rustc::middle::ty::TypeVariants::TyStruct;
 use syntax::ast::*;
-use syntax::codemap::{Span, Spanned};
+use syntax::codemap::Spanned;
+
 use eq_op::is_exp_equal;
-use types::match_ty_unwrap;
 use utils::{match_def_path, span_lint, walk_ptrs_ty, get_parent_expr};
 
 declare_lint! {
index 53d8850c59d46cc9122f1d22034112f65682fdb6..aa5f1d13471f806e42379c2253200f1bfb593ee5 100644 (file)
@@ -1,9 +1,9 @@
-use syntax::ptr::P;
+use rustc::lint::*;
 use syntax::ast;
 use syntax::ast::*;
+use syntax::ptr::P;
 use rustc::middle::ty;
-use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
-use syntax::codemap::{ExpnInfo, Span};
+use syntax::codemap::ExpnInfo;
 
 use utils::{in_macro, snippet, span_lint, span_help_and_lint};
 
@@ -40,6 +40,7 @@ pub fn match_ty_unwrap<'a>(ty: &'a Ty, segments: &[&str]) -> Option<&'a [P<Ty>]>
     }
 }
 
+#[allow(unused_imports)]
 impl LintPass for TypePass {
     fn get_lints(&self) -> LintArray {
         lint_array!(BOX_VEC, LINKEDLIST)
@@ -62,10 +63,8 @@ fn check_ty(&mut self, cx: &Context, ty: &ast::Ty) {
             // In case stuff gets moved around
             use collections::linked_list::LinkedList as DL1;
             use std::collections::linked_list::LinkedList as DL2;
-            use std::collections::linked_list::LinkedList as DL3;
         }
         let dlists = [vec!["std","collections","linked_list","LinkedList"],
-                      vec!["std","collections","linked_list","LinkedList"],
                       vec!["collections","linked_list","LinkedList"]];
         for path in &dlists {
             if match_ty_unwrap(ty, &path[..]).is_some() {
index 62b4a9dadf55ca138989248050d9886228448de6..ab48fd1bef2ddbbea9b888aa4debfe28cc5ee19d 100644 (file)
@@ -1,6 +1,7 @@
 use rustc::lint::*;
 use syntax::ast::*;
 use syntax::codemap::{BytePos, Span};
+
 use utils::span_lint;
 
 declare_lint!{ pub ZERO_WIDTH_SPACE, Deny,
index 67a89b067e6d77f68d9ca4dfee3af2f75c90cff8..47e3a3456d68b7c674b5874fae362971b20cb810 100644 (file)
@@ -1,11 +1,10 @@
-use rustc::lint::{Context, Lint, Level};
-use syntax::ast::{DefId, Expr, Name, NodeId, Path};
+use rustc::lint::*;
+use syntax::ast::*;
 use syntax::codemap::{ExpnInfo, Span};
 use syntax::ptr::P;
 use rustc::ast_map::Node::NodeExpr;
 use rustc::middle::ty;
-use std::borrow::{Cow, IntoCow};
-use std::convert::From;
+use std::borrow::Cow;
 
 /// returns true if the macro that expanded the crate was outside of
 /// the current crate or was a compiler plugin
@@ -35,14 +34,14 @@ pub fn in_external_macro(cx: &Context, span: Span) -> bool {
 /// `match_def_path(cx, id, &["core", "option", "Option"])`
 pub fn match_def_path(cx: &Context, def_id: DefId, path: &[&str]) -> bool {
     cx.tcx.with_path(def_id, |iter| iter.map(|elem| elem.name())
-        .zip(path.iter()).all(|(nm, p)| &nm.as_str() == p))
+        .zip(path.iter()).all(|(nm, p)| nm == p))
 }
 
 /// match a Path against a slice of segment string literals, e.g.
 /// `match_path(path, &["std", "rt", "begin_unwind"])`
 pub fn match_path(path: &Path, segments: &[&str]) -> bool {
     path.segments.iter().rev().zip(segments.iter().rev()).all(
-        |(a,b)| &a.identifier.name.as_str() == b)
+        |(a, b)| &a.identifier.name == b)
 }
 
 /// convert a span to a code snippet if available, otherwise use default, e.g.
index cd4d46ee3276e3244838cf79d1b3229be69a55b9..ea69a8cfa15f6f0c26a5b32e270402b8a7f3cf2a 100644 (file)
@@ -11,5 +11,8 @@ fn the_answer(ref mut x: u8) {  //~ ERROR `ref` directly on a function argument
 fn main() {
   let mut x = 0;
   the_answer(x);
+  // Closures should not warn
+  let y = |ref x| { println!("{:?}", x) };
+  y(1u8);
   println!("The answer is {}.", x);
 }