}
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)),
// 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
/// ```
declare_clippy_lint! {
pub INDEXING_SLICING,
- pedantic,
+ restriction,
"indexing/slicing usage"
}
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,
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,
/// (`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.
///
cx.tcx.sess.crate_types.get().iter().any(|t: &CrateType| {
match t {
- CrateType::CrateTypeExecutable => true,
+ CrateType::Executable => true,
_ => false,
}
})
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.
// 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;
}
// 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) {
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;
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;
/// 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.
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,
}
}
-#![feature(tool_attributes)]
+#![feature(tool_attributes, stmt_expr_attributes)]
fn main() {
#[clippy::author]
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
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
|
57 | let _ = RangeToInclusive { end: end };
| ^^^^^^^^ help: replace it with: `end`
-error: aborting due to 7 previous errors
+error: aborting due to 12 previous errors
#[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) {
&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) {
}
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