]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #2991 from mikerite/issue2979
authorPhilipp Hansch <dev@phansch.net>
Sun, 5 Aug 2018 19:35:55 +0000 (20:35 +0100)
committerGitHub <noreply@github.com>
Sun, 5 Aug 2018 19:35:55 +0000 (20:35 +0100)
Fix #2979

13 files changed:
clippy_lints/src/consts.rs
clippy_lints/src/indexing_slicing.rs
clippy_lints/src/lib.rs
clippy_lints/src/methods.rs
clippy_lints/src/missing_inline.rs
clippy_lints/src/redundant_field_names.rs
clippy_lints/src/trivially_copy_pass_by_ref.rs
clippy_lints/src/utils/mod.rs
tests/ui/author/for_loop.rs
tests/ui/matches.stderr
tests/ui/redundant_field_names.stderr
tests/ui/trivially_copy_pass_by_ref.rs
tests/ui/trivially_copy_pass_by_ref.stderr

index d116ffafeacdcb614439c1e85dc79783f37b5d11..7bccdd7778b443cfb477d6c78f3897a728f8c492 100644 (file)
@@ -426,7 +426,7 @@ fn binop(&mut self, op: BinOp, left: &Expr, right: &Expr) -> Option<Constant> {
 }
 
 pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'tcx>) -> Option<Constant> {
-    use rustc::mir::interpret::{Scalar, ConstValue};
+    use rustc::mir::interpret::{Scalar, ScalarMaybeUndef, ConstValue};
     match result.val {
         ConstValue::Scalar(Scalar::Bits{ bits: b, ..}) => match result.ty.sty {
             ty::TyBool => Some(Constant::Bool(b == 1)),
@@ -436,7 +436,9 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
             // FIXME: implement other conversion
             _ => None,
         },
-        ConstValue::ScalarPair(Scalar::Ptr(ptr), Scalar::Bits { bits: n, .. }) => match result.ty.sty {
+        ConstValue::ScalarPair(Scalar::Ptr(ptr),
+                               ScalarMaybeUndef::Scalar(
+                                Scalar::Bits { bits: n, .. })) => match result.ty.sty {
             ty::TyRef(_, tam, _) => match tam.sty {
                 ty::TyStr => {
                     let alloc = tcx
index 677f59d32cc5a70170821d490d4be95f498c2707..2ad31de0a17b89f71fe7917c7e9acb8b3f11d792 100644 (file)
@@ -80,7 +80,7 @@
 /// ```
 declare_clippy_lint! {
     pub INDEXING_SLICING,
-    pedantic,
+    restriction,
     "indexing/slicing usage"
 }
 
index 4a23de3b8fd234a863d44ab740e11875ea5d1313..31a28c2a0630c4e61241999be3133332c59130a4 100644 (file)
@@ -407,6 +407,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) {
         arithmetic::INTEGER_ARITHMETIC,
         assign_ops::ASSIGN_OPS,
         else_if_without_else::ELSE_IF_WITHOUT_ELSE,
+        indexing_slicing::INDEXING_SLICING,
         inherent_impl::MULTIPLE_INHERENT_IMPL,
         literal_representation::DECIMAL_LITERAL_REPRESENTATION,
         mem_forget::MEM_FORGET,
@@ -437,7 +438,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>) {
         enum_variants::PUB_ENUM_VARIANT_NAMES,
         enum_variants::STUTTER,
         if_not_else::IF_NOT_ELSE,
-        indexing_slicing::INDEXING_SLICING,
         infinite_iter::MAYBE_INFINITE_ITER,
         items_after_statements::ITEMS_AFTER_STATEMENTS,
         matches::SINGLE_MATCH_ELSE,
index 760c1a39bdd1ad9b4df0a211e83cdc1949ea7339..c8f3e0db3240895644170399d9f26ed69c1ab2a9 100644 (file)
 /// (`Rc`, `Arc`, `rc::Weak`, or `sync::Weak`), and suggests calling Clone via unified
 /// function syntax instead (e.g. `Rc::clone(foo)`).
 ///
-/// **Why is this bad?**: Calling '.clone()' on an Rc, Arc, or Weak
+/// **Why is this bad?** Calling '.clone()' on an Rc, Arc, or Weak
 /// can obscure the fact that only the pointer is being cloned, not the underlying
 /// data.
 ///
index e19ec4da67ed29cd4352924181c908dc96e41b9c..e80a0a1d8c3ecc3b9c60d0361599e6a0012ddd17 100644 (file)
@@ -87,7 +87,7 @@ fn is_executable<'a, 'tcx>(cx: &LateContext<'a, 'tcx>) -> bool {
 
     cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| {
         match t {
-            CrateType::CrateTypeExecutable => true,
+            CrateType::Executable => true,
             _ => false,
         }
     })
index 4f28d36e2a8f72e66533d11f1ca709be7b937a84..27f890ccd90fc5e6db0e035d43631ee9d10fe514 100644 (file)
@@ -1,7 +1,7 @@
 use rustc::lint::*;
 use rustc::{declare_lint, lint_array};
 use rustc::hir::*;
-use crate::utils::{in_macro, is_range_expression, match_var, span_lint_and_sugg};
+use crate::utils::{in_macro, match_var, span_lint_and_sugg};
 
 /// **What it does:** Checks for fields in struct literals where shorthands
 /// could be used.
@@ -40,7 +40,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         // Ignore all macros including range expressions.
         // They can have redundant field names when expanded.
         // e.g. range expression `start..end` is desugared to `Range { start: start, end: end }`
-        if in_macro(expr.span) || is_range_expression(expr.span) {
+        if in_macro(expr.span) {
             return;
         }
 
index 6a048b1921384b87af6f201cd32d1e132233b2a2..a01a1c5ae31bd241267bf942497ef21d5ee74a88 100644 (file)
@@ -124,10 +124,10 @@ fn check_fn(
         // Use lifetimes to determine if we're returning a reference to the
         // argument. In that case we can't switch to pass-by-value as the
         // argument will not live long enough.
-        let output_lt = if let TypeVariants::TyRef(output_lt, _, _) = fn_sig.output().sty {
-            Some(output_lt)
-        } else {
-            None
+        let output_lts = match fn_sig.output().sty {
+            TypeVariants::TyRef(output_lt, _, _) => vec![output_lt],
+            TypeVariants::TyAdt(_, substs) => substs.regions().collect(),
+            _ => vec![],
         };
 
         for ((input, &ty), arg) in decl.inputs.iter().zip(fn_sig.inputs()).zip(&body.arguments) {
@@ -138,7 +138,7 @@ fn check_fn(
 
             if_chain! {
                 if let TypeVariants::TyRef(input_lt, ty, Mutability::MutImmutable) = ty.sty;
-                if Some(input_lt) != output_lt;
+                if !output_lts.contains(&input_lt);
                 if is_copy(cx, ty);
                 if let Some(size) = cx.layout_of(ty).ok().map(|l| l.size.bytes());
                 if size <= self.limit;
index 0b2103ca7eaa505ab71a1ebe88189fe6252b4c95..0bb06fce593046d21997a30c70c6bb6a8b2d8b80 100644 (file)
@@ -19,7 +19,7 @@
 use std::rc::Rc;
 use syntax::ast::{self, LitKind};
 use syntax::attr;
-use syntax::codemap::{CompilerDesugaringKind, ExpnFormat, Span, DUMMY_SP};
+use syntax::codemap::{Span, DUMMY_SP};
 use syntax::errors::DiagnosticBuilder;
 use syntax::ptr::P;
 use syntax::symbol::keywords;
@@ -58,23 +58,7 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: NodeId) -> bool {
 
 /// Returns true if this `expn_info` was expanded by any macro.
 pub fn in_macro(span: Span) -> bool {
-    span.ctxt().outer().expn_info().map_or(false, |info| {
-        match info.format {
-            // don't treat range expressions desugared to structs as "in_macro"
-            ExpnFormat::CompilerDesugaring(kind) => kind != CompilerDesugaringKind::DotFill,
-            _ => true,
-        }
-    })
-}
-
-/// Returns true if `expn_info` was expanded by range expressions.
-pub fn is_range_expression(span: Span) -> bool {
-    span.ctxt().outer().expn_info().map_or(false, |info| {
-        match info.format {
-            ExpnFormat::CompilerDesugaring(CompilerDesugaringKind::DotFill) => true,
-            _ => false,
-        }
-    })
+    span.ctxt().outer().expn_info().is_some()
 }
 
 /// Check if a `DefId`'s path matches the given absolute type path usage.
@@ -957,7 +941,8 @@ pub fn opt_def_id(def: Def) -> Option<DefId> {
         Def::AssociatedExistential(id) |
         Def::GlobalAsm(id) => Some(id),
 
-        Def::Upvar(..) | Def::Local(_) | Def::Label(..) | Def::PrimTy(..) | Def::SelfTy(..) | Def::Err => None,
+        Def::Upvar(..) | Def::Local(_) | Def::Label(..) | Def::PrimTy(..) | Def::SelfTy(..) |
+        Def::ToolMod | Def::NonMacroAttr | Def::Err => None,
     }
 }
 
index 5faf440676d09444e0691bcc00b2e7ba5696acdf..026aee4746d157ae6d7c3f2d7a4b755ff663b6cb 100644 (file)
@@ -1,4 +1,4 @@
-#![feature(tool_attributes)]
+#![feature(tool_attributes, stmt_expr_attributes)]
 
 fn main() {
     #[clippy::author]
index 5bfc3271c4506c3f64106946de46402b55eeb8cd..61c13056ccadebd625e7c612ac9c282a6e3b67dc 100644 (file)
@@ -33,15 +33,7 @@ error: you seem to be trying to use match for destructuring a single pattern. Co
 41 | |         &(v, 1) => println!("{}", v),
 42 | |         _ => println!("none"),
 43 | |     }
-   | |_____^
-help: try this
-   |
-40 |     if let &(v, 1) = tup {
-41 | # [ cfg ( not ( stage0 ) ) ] {
-42 | ( $ crate :: io :: _print ( format_args_nl ! ( $ ( $ arg ) * ) ) ) ; } # [
-43 | cfg ( stage0 ) ] { print ! ( "{}/n" , format_args ! ( $ ( $ arg ) * ) ) } } else {
-44 | ( $ crate :: io :: _print ( format_args_nl ! ( $ ( $ arg ) * ) ) ) ; }
-   |
+   | |_____^ help: try this: `if let &(v, 1) = tup { $ crate :: io :: _print ( format_args_nl ! ( $ ( $ arg ) * ) ) ; } else { $ crate :: io :: _print ( format_args_nl ! ( $ ( $ arg ) * ) ) ; }`
 
 error: you don't need to add `&` to all patterns
   --> $DIR/matches.rs:40:5
index d757f1871a781c4026b24485163b0cd050ef837f..4f706d1fe08585bb7d7a181e691489361011f256 100644 (file)
@@ -12,6 +12,36 @@ error: redundant field names in struct initialization
 35 |         age: age,
    |         ^^^^^^^^ help: replace it with: `age`
 
+error: redundant field names in struct initialization
+  --> $DIR/redundant_field_names.rs:45:13
+   |
+45 |     let _ = start..;
+   |             ^^^^^ help: replace it with: `start`
+
+error: redundant field names in struct initialization
+  --> $DIR/redundant_field_names.rs:46:15
+   |
+46 |     let _ = ..end;
+   |               ^^^ help: replace it with: `end`
+
+error: redundant field names in struct initialization
+  --> $DIR/redundant_field_names.rs:47:13
+   |
+47 |     let _ = start..end;
+   |             ^^^^^ help: replace it with: `start`
+
+error: redundant field names in struct initialization
+  --> $DIR/redundant_field_names.rs:47:20
+   |
+47 |     let _ = start..end;
+   |                    ^^^ help: replace it with: `end`
+
+error: redundant field names in struct initialization
+  --> $DIR/redundant_field_names.rs:49:16
+   |
+49 |     let _ = ..=end;
+   |                ^^^ help: replace it with: `end`
+
 error: redundant field names in struct initialization
   --> $DIR/redundant_field_names.rs:53:25
    |
@@ -42,5 +72,5 @@ error: redundant field names in struct initialization
 57 |     let _ = RangeToInclusive { end: end };
    |                                ^^^^^^^^ help: replace it with: `end`
 
-error: aborting due to 7 previous errors
+error: aborting due to 12 previous errors
 
index c6773add244305186f2b8657174633e260464ffe..9b905e8d62888ebe60b4eaffe67a5c29df98df06 100644 (file)
@@ -6,6 +6,10 @@
 #[derive(Copy, Clone)]
 struct Bar([u8; 24]);
 
+struct FooRef<'a> {
+    foo: &'a Foo,
+}
+
 type Baz = u32;
 
 fn good(a: &mut u32, b: u32, c: &Bar) {
@@ -20,6 +24,19 @@ fn good_return_explicit_lt_ref<'a>(foo: &'a Foo) -> &'a u32 {
     &foo.0
 }
 
+fn good_return_implicit_lt_struct(foo: &Foo) -> FooRef {
+    FooRef {
+        foo,
+    }
+}
+
+#[allow(needless_lifetimes)]
+fn good_return_explicit_lt_struct<'a>(foo: &'a Foo) -> FooRef<'a> {
+    FooRef {
+        foo,
+    }
+}
+
 fn bad(x: &u32, y: &Foo, z: &Baz) {
 }
 
index db25cc5a02011b2a278ef7e580948c88a9dad3ab..757b6b4c9a98e2838a94168f26772b575c82a327 100644 (file)
@@ -1,81 +1,81 @@
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:23:11
+  --> $DIR/trivially_copy_pass_by_ref.rs:40:11
    |
-23 | fn bad(x: &u32, y: &Foo, z: &Baz) {
+40 | fn bad(x: &u32, y: &Foo, z: &Baz) {
    |           ^^^^ help: consider passing by value instead: `u32`
    |
    = note: `-D trivially-copy-pass-by-ref` implied by `-D warnings`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:23:20
+  --> $DIR/trivially_copy_pass_by_ref.rs:40:20
    |
-23 | fn bad(x: &u32, y: &Foo, z: &Baz) {
+40 | fn bad(x: &u32, y: &Foo, z: &Baz) {
    |                    ^^^^ help: consider passing by value instead: `Foo`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:23:29
+  --> $DIR/trivially_copy_pass_by_ref.rs:40:29
    |
-23 | fn bad(x: &u32, y: &Foo, z: &Baz) {
+40 | fn bad(x: &u32, y: &Foo, z: &Baz) {
    |                             ^^^^ help: consider passing by value instead: `Baz`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:33:12
+  --> $DIR/trivially_copy_pass_by_ref.rs:50:12
    |
-33 |     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
+50 |     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
    |            ^^^^^ help: consider passing by value instead: `self`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:33:22
+  --> $DIR/trivially_copy_pass_by_ref.rs:50:22
    |
-33 |     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
+50 |     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
    |                      ^^^^ help: consider passing by value instead: `u32`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:33:31
+  --> $DIR/trivially_copy_pass_by_ref.rs:50:31
    |
-33 |     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
+50 |     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
    |                               ^^^^ help: consider passing by value instead: `Foo`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:33:40
+  --> $DIR/trivially_copy_pass_by_ref.rs:50:40
    |
-33 |     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
+50 |     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
    |                                        ^^^^ help: consider passing by value instead: `Baz`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:36:16
+  --> $DIR/trivially_copy_pass_by_ref.rs:53:16
    |
-36 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
+53 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
    |                ^^^^ help: consider passing by value instead: `u32`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:36:25
+  --> $DIR/trivially_copy_pass_by_ref.rs:53:25
    |
-36 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
+53 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
    |                         ^^^^ help: consider passing by value instead: `Foo`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:36:34
+  --> $DIR/trivially_copy_pass_by_ref.rs:53:34
    |
-36 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
+53 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
    |                                  ^^^^ help: consider passing by value instead: `Baz`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:50:16
+  --> $DIR/trivially_copy_pass_by_ref.rs:67:16
    |
-50 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
+67 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
    |                ^^^^ help: consider passing by value instead: `u32`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:50:25
+  --> $DIR/trivially_copy_pass_by_ref.rs:67:25
    |
-50 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
+67 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
    |                         ^^^^ help: consider passing by value instead: `Foo`
 
 error: this argument is passed by reference, but would be more efficient if passed by value
-  --> $DIR/trivially_copy_pass_by_ref.rs:50:34
+  --> $DIR/trivially_copy_pass_by_ref.rs:67:34
    |
-50 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
+67 |     fn bad2(x: &u32, y: &Foo, z: &Baz) {
    |                                  ^^^^ help: consider passing by value instead: `Baz`
 
 error: aborting due to 13 previous errors