Run rustfmt
This commit is contained in:
parent
35eda0531a
commit
2544458559
16 changed files with 175 additions and 131 deletions
|
@ -97,19 +97,25 @@ fn get_pat_name(pat: &Pat) -> Option<Name> {
|
||||||
match pat.node {
|
match pat.node {
|
||||||
PatKind::Binding(_, _, ref spname, _) => Some(spname.node),
|
PatKind::Binding(_, _, ref spname, _) => Some(spname.node),
|
||||||
PatKind::Path(ref qpath) => single_segment_path(qpath).map(|ps| ps.name),
|
PatKind::Path(ref qpath) => single_segment_path(qpath).map(|ps| ps.name),
|
||||||
PatKind::Box(ref p) | PatKind::Ref(ref p, _) => get_pat_name(&*p),
|
PatKind::Box(ref p) |
|
||||||
_ => None
|
PatKind::Ref(ref p, _) => get_pat_name(&*p),
|
||||||
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_path_name(expr: &Expr) -> Option<Name> {
|
fn get_path_name(expr: &Expr) -> Option<Name> {
|
||||||
match expr.node {
|
match expr.node {
|
||||||
ExprBox(ref e) | ExprAddrOf(_, ref e) | ExprUnary(UnOp::UnDeref, ref e) => get_path_name(e),
|
ExprBox(ref e) |
|
||||||
ExprBlock(ref b) => if b.stmts.is_empty() {
|
ExprAddrOf(_, ref e) |
|
||||||
b.expr.as_ref().and_then(|p| get_path_name(p))
|
ExprUnary(UnOp::UnDeref, ref e) => get_path_name(e),
|
||||||
} else { None },
|
ExprBlock(ref b) => {
|
||||||
|
if b.stmts.is_empty() {
|
||||||
|
b.expr.as_ref().and_then(|p| get_path_name(p))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
},
|
||||||
ExprPath(ref qpath) => single_segment_path(qpath).map(|ps| ps.name),
|
ExprPath(ref qpath) => single_segment_path(qpath).map(|ps| ps.name),
|
||||||
_ => None
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,8 @@ declare_lint! {
|
||||||
/// [using `|`](https://doc.rust-lang.org/book/patterns.html#multiple-patterns).
|
/// [using `|`](https://doc.rust-lang.org/book/patterns.html#multiple-patterns).
|
||||||
///
|
///
|
||||||
/// **Known problems:** False positive possible with order dependent `match`
|
/// **Known problems:** False positive possible with order dependent `match`
|
||||||
/// (see issue [#860](https://github.com/rust-lang-nursery/rust-clippy/issues/860)).
|
/// (see issue
|
||||||
|
/// [#860](https://github.com/rust-lang-nursery/rust-clippy/issues/860)).
|
||||||
///
|
///
|
||||||
/// **Example:**
|
/// **Example:**
|
||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
|
|
|
@ -94,10 +94,7 @@ pub fn strip_doc_comment_decoration(comment: &str, span: Span) -> (String, Vec<(
|
||||||
return (
|
return (
|
||||||
doc.to_owned(),
|
doc.to_owned(),
|
||||||
vec![
|
vec![
|
||||||
(
|
(doc.len(), span.with_lo(span.lo() + BytePos(prefix.len() as u32))),
|
||||||
doc.len(),
|
|
||||||
span.with_lo(span.lo() + BytePos(prefix.len() as u32)),
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -112,10 +109,7 @@ pub fn strip_doc_comment_decoration(comment: &str, span: Span) -> (String, Vec<(
|
||||||
debug_assert_eq!(offset as u32 as usize, offset);
|
debug_assert_eq!(offset as u32 as usize, offset);
|
||||||
contains_initial_stars |= line.trim_left().starts_with('*');
|
contains_initial_stars |= line.trim_left().starts_with('*');
|
||||||
// +1 for the newline
|
// +1 for the newline
|
||||||
sizes.push((
|
sizes.push((line.len() + 1, span.with_lo(span.lo() + BytePos(offset as u32))));
|
||||||
line.len() + 1,
|
|
||||||
span.with_lo(span.lo() + BytePos(offset as u32)),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
if !contains_initial_stars {
|
if !contains_initial_stars {
|
||||||
return (doc.to_string(), sizes);
|
return (doc.to_string(), sizes);
|
||||||
|
|
|
@ -50,9 +50,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
||||||
let (lint, msg) = match complete_infinite_iter(cx, expr) {
|
let (lint, msg) = match complete_infinite_iter(cx, expr) {
|
||||||
Infinite => (INFINITE_ITER, "infinite iteration detected"),
|
Infinite => (INFINITE_ITER, "infinite iteration detected"),
|
||||||
MaybeInfinite => (MAYBE_INFINITE_ITER,
|
MaybeInfinite => (MAYBE_INFINITE_ITER, "possible infinite iteration detected"),
|
||||||
"possible infinite iteration detected"),
|
Finite => {
|
||||||
Finite => { return; }
|
return;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
span_lint(cx, lint, expr.span, msg)
|
span_lint(cx, lint, expr.span, msg)
|
||||||
}
|
}
|
||||||
|
@ -62,7 +63,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
||||||
enum Finiteness {
|
enum Finiteness {
|
||||||
Infinite,
|
Infinite,
|
||||||
MaybeInfinite,
|
MaybeInfinite,
|
||||||
Finite
|
Finite,
|
||||||
}
|
}
|
||||||
|
|
||||||
use self::Finiteness::{Infinite, MaybeInfinite, Finite};
|
use self::Finiteness::{Infinite, MaybeInfinite, Finite};
|
||||||
|
@ -71,16 +72,18 @@ impl Finiteness {
|
||||||
fn and(self, b: Self) -> Self {
|
fn and(self, b: Self) -> Self {
|
||||||
match (self, b) {
|
match (self, b) {
|
||||||
(Finite, _) | (_, Finite) => Finite,
|
(Finite, _) | (_, Finite) => Finite,
|
||||||
(MaybeInfinite, _) | (_, MaybeInfinite) => MaybeInfinite,
|
(MaybeInfinite, _) |
|
||||||
_ => Infinite
|
(_, MaybeInfinite) => MaybeInfinite,
|
||||||
|
_ => Infinite,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn or(self, b: Self) -> Self {
|
fn or(self, b: Self) -> Self {
|
||||||
match (self, b) {
|
match (self, b) {
|
||||||
(Infinite, _) | (_, Infinite) => Infinite,
|
(Infinite, _) | (_, Infinite) => Infinite,
|
||||||
(MaybeInfinite, _) | (_, MaybeInfinite) => MaybeInfinite,
|
(MaybeInfinite, _) |
|
||||||
_ => Finite
|
(_, MaybeInfinite) => MaybeInfinite,
|
||||||
|
_ => Finite,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,7 +105,7 @@ enum Heuristic {
|
||||||
/// infinite if any of the supplied arguments is
|
/// infinite if any of the supplied arguments is
|
||||||
Any,
|
Any,
|
||||||
/// infinite if all of the supplied arguments are
|
/// infinite if all of the supplied arguments are
|
||||||
All
|
All,
|
||||||
}
|
}
|
||||||
|
|
||||||
use self::Heuristic::{Always, First, Any, All};
|
use self::Heuristic::{Always, First, Any, All};
|
||||||
|
@ -112,7 +115,7 @@ use self::Heuristic::{Always, First, Any, All};
|
||||||
/// returns an infinite or possibly infinite iterator. The finiteness
|
/// returns an infinite or possibly infinite iterator. The finiteness
|
||||||
/// is an upper bound, e.g. some methods can return a possibly
|
/// is an upper bound, e.g. some methods can return a possibly
|
||||||
/// infinite iterator at worst, e.g. `take_while`.
|
/// infinite iterator at worst, e.g. `take_while`.
|
||||||
static HEURISTICS : &[(&str, usize, Heuristic, Finiteness)] = &[
|
static HEURISTICS: &[(&str, usize, Heuristic, Finiteness)] = &[
|
||||||
("zip", 2, All, Infinite),
|
("zip", 2, All, Infinite),
|
||||||
("chain", 2, Any, Infinite),
|
("chain", 2, Any, Infinite),
|
||||||
("cycle", 1, Always, Infinite),
|
("cycle", 1, Always, Infinite),
|
||||||
|
@ -131,7 +134,7 @@ static HEURISTICS : &[(&str, usize, Heuristic, Finiteness)] = &[
|
||||||
("flat_map", 2, First, Infinite),
|
("flat_map", 2, First, Infinite),
|
||||||
("unzip", 1, First, Infinite),
|
("unzip", 1, First, Infinite),
|
||||||
("take_while", 2, First, MaybeInfinite),
|
("take_while", 2, First, MaybeInfinite),
|
||||||
("scan", 3, First, MaybeInfinite)
|
("scan", 3, First, MaybeInfinite),
|
||||||
];
|
];
|
||||||
|
|
||||||
fn is_infinite(cx: &LateContext, expr: &Expr) -> Finiteness {
|
fn is_infinite(cx: &LateContext, expr: &Expr) -> Finiteness {
|
||||||
|
@ -140,11 +143,11 @@ fn is_infinite(cx: &LateContext, expr: &Expr) -> Finiteness {
|
||||||
for &(name, len, heuristic, cap) in HEURISTICS.iter() {
|
for &(name, len, heuristic, cap) in HEURISTICS.iter() {
|
||||||
if method.name == name && args.len() == len {
|
if method.name == name && args.len() == len {
|
||||||
return (match heuristic {
|
return (match heuristic {
|
||||||
Always => Infinite,
|
Always => Infinite,
|
||||||
First => is_infinite(cx, &args[0]),
|
First => is_infinite(cx, &args[0]),
|
||||||
Any => is_infinite(cx, &args[0]).or(is_infinite(cx, &args[1])),
|
Any => is_infinite(cx, &args[0]).or(is_infinite(cx, &args[1])),
|
||||||
All => is_infinite(cx, &args[0]).and(is_infinite(cx, &args[1])),
|
All => is_infinite(cx, &args[0]).and(is_infinite(cx, &args[1])),
|
||||||
}).and(cap);
|
}).and(cap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if method.name == "flat_map" && args.len() == 2 {
|
if method.name == "flat_map" && args.len() == 2 {
|
||||||
|
@ -155,35 +158,39 @@ fn is_infinite(cx: &LateContext, expr: &Expr) -> Finiteness {
|
||||||
}
|
}
|
||||||
Finite
|
Finite
|
||||||
},
|
},
|
||||||
ExprBlock(ref block) =>
|
ExprBlock(ref block) => block.expr.as_ref().map_or(Finite, |e| is_infinite(cx, e)),
|
||||||
block.expr.as_ref().map_or(Finite, |e| is_infinite(cx, e)),
|
ExprBox(ref e) |
|
||||||
ExprBox(ref e) | ExprAddrOf(_, ref e) => is_infinite(cx, e),
|
ExprAddrOf(_, ref e) => is_infinite(cx, e),
|
||||||
ExprCall(ref path, _) => {
|
ExprCall(ref path, _) => {
|
||||||
if let ExprPath(ref qpath) = path.node {
|
if let ExprPath(ref qpath) = path.node {
|
||||||
match_qpath(qpath, &paths::REPEAT).into()
|
match_qpath(qpath, &paths::REPEAT).into()
|
||||||
} else { Finite }
|
} else {
|
||||||
|
Finite
|
||||||
|
}
|
||||||
},
|
},
|
||||||
ExprStruct(..) => {
|
ExprStruct(..) => {
|
||||||
higher::range(expr).map_or(false, |r| r.end.is_none()).into()
|
higher::range(expr)
|
||||||
|
.map_or(false, |r| r.end.is_none())
|
||||||
|
.into()
|
||||||
},
|
},
|
||||||
_ => Finite
|
_ => Finite,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// the names and argument lengths of methods that *may* exhaust their
|
/// the names and argument lengths of methods that *may* exhaust their
|
||||||
/// iterators
|
/// iterators
|
||||||
static POSSIBLY_COMPLETING_METHODS : &[(&str, usize)] = &[
|
static POSSIBLY_COMPLETING_METHODS: &[(&str, usize)] = &[
|
||||||
("find", 2),
|
("find", 2),
|
||||||
("rfind", 2),
|
("rfind", 2),
|
||||||
("position", 2),
|
("position", 2),
|
||||||
("rposition", 2),
|
("rposition", 2),
|
||||||
("any", 2),
|
("any", 2),
|
||||||
("all", 2)
|
("all", 2),
|
||||||
];
|
];
|
||||||
|
|
||||||
/// the names and argument lengths of methods that *always* exhaust
|
/// the names and argument lengths of methods that *always* exhaust
|
||||||
/// their iterators
|
/// their iterators
|
||||||
static COMPLETING_METHODS : &[(&str, usize)] = &[
|
static COMPLETING_METHODS: &[(&str, usize)] = &[
|
||||||
("count", 1),
|
("count", 1),
|
||||||
("collect", 1),
|
("collect", 1),
|
||||||
("fold", 3),
|
("fold", 3),
|
||||||
|
@ -196,7 +203,7 @@ static COMPLETING_METHODS : &[(&str, usize)] = &[
|
||||||
("min_by", 2),
|
("min_by", 2),
|
||||||
("min_by_key", 2),
|
("min_by_key", 2),
|
||||||
("sum", 1),
|
("sum", 1),
|
||||||
("product", 1)
|
("product", 1),
|
||||||
];
|
];
|
||||||
|
|
||||||
fn complete_infinite_iter(cx: &LateContext, expr: &Expr) -> Finiteness {
|
fn complete_infinite_iter(cx: &LateContext, expr: &Expr) -> Finiteness {
|
||||||
|
@ -213,20 +220,24 @@ fn complete_infinite_iter(cx: &LateContext, expr: &Expr) -> Finiteness {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if method.name == "last" && args.len() == 1 &&
|
if method.name == "last" && args.len() == 1 &&
|
||||||
get_trait_def_id(cx, &paths::DOUBLE_ENDED_ITERATOR).map_or(false,
|
get_trait_def_id(cx, &paths::DOUBLE_ENDED_ITERATOR).map_or(
|
||||||
|id| !implements_trait(cx,
|
false,
|
||||||
cx.tables.expr_ty(&args[0]),
|
|id| {
|
||||||
id,
|
!implements_trait(cx, cx.tables.expr_ty(&args[0]), id, &[])
|
||||||
&[])) {
|
},
|
||||||
|
)
|
||||||
|
{
|
||||||
return is_infinite(cx, &args[0]);
|
return is_infinite(cx, &args[0]);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ExprBinary(op, ref l, ref r) => {
|
ExprBinary(op, ref l, ref r) => {
|
||||||
if op.node.is_comparison() {
|
if op.node.is_comparison() {
|
||||||
return is_infinite(cx, l).and(is_infinite(cx, r)).and(MaybeInfinite)
|
return is_infinite(cx, l).and(is_infinite(cx, r)).and(
|
||||||
|
MaybeInfinite,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}, //TODO: ExprLoop + Match
|
}, //TODO: ExprLoop + Match
|
||||||
_ => ()
|
_ => (),
|
||||||
}
|
}
|
||||||
Finite
|
Finite
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ impl EarlyLintPass for UnitExpr {
|
||||||
cx,
|
cx,
|
||||||
UNIT_EXPR,
|
UNIT_EXPR,
|
||||||
expr.span,
|
expr.span,
|
||||||
"This expression evaluates to the Unit type ()",
|
"This expression evaluates to the Unit type ()",
|
||||||
span,
|
span,
|
||||||
"Consider removing the trailing semicolon",
|
"Consider removing the trailing semicolon",
|
||||||
);
|
);
|
||||||
|
@ -100,10 +100,12 @@ impl EarlyLintPass for UnitExpr {
|
||||||
}
|
}
|
||||||
fn is_unit_expr(expr: &Expr) -> Option<Span> {
|
fn is_unit_expr(expr: &Expr) -> Option<Span> {
|
||||||
match expr.node {
|
match expr.node {
|
||||||
ExprKind::Block(ref block) => if check_last_stmt_in_block(block) {
|
ExprKind::Block(ref block) => {
|
||||||
Some(block.stmts[block.stmts.len() - 1].span)
|
if check_last_stmt_in_block(block) {
|
||||||
} else {
|
Some(block.stmts[block.stmts.len() - 1].span)
|
||||||
None
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
},
|
},
|
||||||
ExprKind::If(_, ref then, ref else_) => {
|
ExprKind::If(_, ref then, ref else_) => {
|
||||||
let check_then = check_last_stmt_in_block(then);
|
let check_then = check_last_stmt_in_block(then);
|
||||||
|
@ -113,11 +115,7 @@ fn is_unit_expr(expr: &Expr) -> Option<Span> {
|
||||||
return Some(*expr_else);
|
return Some(*expr_else);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if check_then {
|
if check_then { Some(expr.span) } else { None }
|
||||||
Some(expr.span)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
ExprKind::Match(ref _pattern, ref arms) => {
|
ExprKind::Match(ref _pattern, ref arms) => {
|
||||||
for arm in arms {
|
for arm in arms {
|
||||||
|
@ -135,12 +133,16 @@ fn check_last_stmt_in_block(block: &Block) -> bool {
|
||||||
let final_stmt = &block.stmts[block.stmts.len() - 1];
|
let final_stmt = &block.stmts[block.stmts.len() - 1];
|
||||||
|
|
||||||
|
|
||||||
//Made a choice here to risk false positives on divergent macro invocations like `panic!()`
|
// Made a choice here to risk false positives on divergent macro invocations
|
||||||
|
// like `panic!()`
|
||||||
match final_stmt.node {
|
match final_stmt.node {
|
||||||
StmtKind::Expr(_) => false,
|
StmtKind::Expr(_) => false,
|
||||||
StmtKind::Semi(ref expr) => match expr.node {
|
StmtKind::Semi(ref expr) => {
|
||||||
ExprKind::Break(_, _) | ExprKind::Ret(_) => false,
|
match expr.node {
|
||||||
_ => true,
|
ExprKind::Break(_, _) |
|
||||||
|
ExprKind::Ret(_) => false,
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
_ => true,
|
_ => true,
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,11 +121,7 @@ fn check_trait_items(cx: &LateContext, visited_trait: &Item, trait_items: &[Trai
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cx.access_levels.is_exported(visited_trait.id) &&
|
if cx.access_levels.is_exported(visited_trait.id) && trait_items.iter().any(|i| is_named_self(cx, i, "len")) {
|
||||||
trait_items
|
|
||||||
.iter()
|
|
||||||
.any(|i| is_named_self(cx, i, "len"))
|
|
||||||
{
|
|
||||||
let mut current_and_super_traits = HashSet::new();
|
let mut current_and_super_traits = HashSet::new();
|
||||||
fill_trait_set(visited_trait, &mut current_and_super_traits, cx);
|
fill_trait_set(visited_trait, &mut current_and_super_traits, cx);
|
||||||
|
|
||||||
|
|
|
@ -1463,14 +1463,15 @@ fn is_as_ref_or_mut_trait(ty: &hir::Ty, self_ty: &hir::Ty, generics: &hir::Gener
|
||||||
if let hir::TyParamBound::TraitTyParamBound(ref ptr, ..) = *bound {
|
if let hir::TyParamBound::TraitTyParamBound(ref ptr, ..) = *bound {
|
||||||
let path = &ptr.trait_ref.path;
|
let path = &ptr.trait_ref.path;
|
||||||
match_path(path, name) &&
|
match_path(path, name) &&
|
||||||
path.segments.last().map_or(false, |s| {
|
path.segments.last().map_or(
|
||||||
if s.parameters.parenthesized {
|
false,
|
||||||
|
|s| if s.parameters.parenthesized {
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
s.parameters.types.len() == 1 &&
|
s.parameters.types.len() == 1 &&
|
||||||
(is_self_ty(&s.parameters.types[0]) || is_ty(&*s.parameters.types[0], self_ty))
|
(is_self_ty(&s.parameters.types[0]) || is_ty(&*s.parameters.types[0], self_ty))
|
||||||
}
|
},
|
||||||
})
|
)
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
|
//
|
||||||
// rs#L246
|
// rs#L246
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,8 @@ use utils::{span_lint_and_then, in_macro, snippet};
|
||||||
|
|
||||||
/// **What it does:** Checks for useless borrowed references.
|
/// **What it does:** Checks for useless borrowed references.
|
||||||
///
|
///
|
||||||
/// **Why is this bad?** It is mostly useless and make the code look more complex than it
|
/// **Why is this bad?** It is mostly useless and make the code look more
|
||||||
|
/// complex than it
|
||||||
/// actually is.
|
/// actually is.
|
||||||
///
|
///
|
||||||
/// **Known problems:** It seems that the `&ref` pattern is sometimes useful.
|
/// **Known problems:** It seems that the `&ref` pattern is sometimes useful.
|
||||||
|
@ -21,12 +22,14 @@ use utils::{span_lint_and_then, in_macro, snippet};
|
||||||
///
|
///
|
||||||
/// fn foo(a: &Animal, b: &Animal) {
|
/// fn foo(a: &Animal, b: &Animal) {
|
||||||
/// match (a, b) {
|
/// match (a, b) {
|
||||||
/// (&Animal::Cat(v), k) | (k, &Animal::Cat(v)) => (), // lifetime mismatch error
|
/// (&Animal::Cat(v), k) | (k, &Animal::Cat(v)) => (), // lifetime
|
||||||
|
/// mismatch error
|
||||||
/// (&Animal::Dog(ref c), &Animal::Dog(_)) => ()
|
/// (&Animal::Dog(ref c), &Animal::Dog(_)) => ()
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
/// There is a lifetime mismatch error for `k` (indeed a and b have distinct lifetime).
|
/// There is a lifetime mismatch error for `k` (indeed a and b have distinct
|
||||||
|
/// lifetime).
|
||||||
/// This can be fixed by using the `&ref` pattern.
|
/// This can be fixed by using the `&ref` pattern.
|
||||||
/// However, the code can also be fixed by much cleaner ways
|
/// However, the code can also be fixed by much cleaner ways
|
||||||
///
|
///
|
||||||
|
@ -77,4 +80,3 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrowedRef {
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -139,11 +139,7 @@ fn str_span(base: Span, s: &str, c: usize) -> Span {
|
||||||
|
|
||||||
match (si.next(), si.next()) {
|
match (si.next(), si.next()) {
|
||||||
(Some((l, _)), Some((h, _))) => {
|
(Some((l, _)), Some((h, _))) => {
|
||||||
Span::new(
|
Span::new(base.lo() + BytePos(l as u32), base.lo() + BytePos(h as u32), base.ctxt())
|
||||||
base.lo() + BytePos(l as u32),
|
|
||||||
base.lo() + BytePos(h as u32),
|
|
||||||
base.ctxt(),
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
_ => base,
|
_ => base,
|
||||||
}
|
}
|
||||||
|
|
|
@ -390,4 +390,3 @@ fn is_self_shadow(name: Name, expr: &Expr) -> bool {
|
||||||
fn path_eq_name(name: Name, path: &Path) -> bool {
|
fn path_eq_name(name: Name, path: &Path) -> bool {
|
||||||
!path.is_global() && path.segments.len() == 1 && path.segments[0].name.as_str() == name.as_str()
|
!path.is_global() && path.segments.len() == 1 && path.segments[0].name.as_str() == name.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -182,12 +182,18 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) {
|
||||||
match *qpath {
|
match *qpath {
|
||||||
QPath::Resolved(Some(ref ty), ref p) => {
|
QPath::Resolved(Some(ref ty), ref p) => {
|
||||||
check_ty(cx, ty, is_local);
|
check_ty(cx, ty, is_local);
|
||||||
for ty in p.segments.iter().flat_map(|seg| seg.parameters.types.iter()) {
|
for ty in p.segments.iter().flat_map(
|
||||||
|
|seg| seg.parameters.types.iter(),
|
||||||
|
)
|
||||||
|
{
|
||||||
check_ty(cx, ty, is_local);
|
check_ty(cx, ty, is_local);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
QPath::Resolved(None, ref p) => {
|
QPath::Resolved(None, ref p) => {
|
||||||
for ty in p.segments.iter().flat_map(|seg| seg.parameters.types.iter()) {
|
for ty in p.segments.iter().flat_map(
|
||||||
|
|seg| seg.parameters.types.iter(),
|
||||||
|
)
|
||||||
|
{
|
||||||
check_ty(cx, ty, is_local);
|
check_ty(cx, ty, is_local);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -523,21 +529,25 @@ declare_lint! {
|
||||||
/// Will return 0 if the type is not an int or uint variant
|
/// Will return 0 if the type is not an int or uint variant
|
||||||
fn int_ty_to_nbits(typ: Ty, tcx: TyCtxt) -> u64 {
|
fn int_ty_to_nbits(typ: Ty, tcx: TyCtxt) -> u64 {
|
||||||
match typ.sty {
|
match typ.sty {
|
||||||
ty::TyInt(i) => match i {
|
ty::TyInt(i) => {
|
||||||
IntTy::Is => tcx.data_layout.pointer_size.bits(),
|
match i {
|
||||||
IntTy::I8 => 8,
|
IntTy::Is => tcx.data_layout.pointer_size.bits(),
|
||||||
IntTy::I16 => 16,
|
IntTy::I8 => 8,
|
||||||
IntTy::I32 => 32,
|
IntTy::I16 => 16,
|
||||||
IntTy::I64 => 64,
|
IntTy::I32 => 32,
|
||||||
IntTy::I128 => 128,
|
IntTy::I64 => 64,
|
||||||
|
IntTy::I128 => 128,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
ty::TyUint(i) => match i {
|
ty::TyUint(i) => {
|
||||||
UintTy::Us => tcx.data_layout.pointer_size.bits(),
|
match i {
|
||||||
UintTy::U8 => 8,
|
UintTy::Us => tcx.data_layout.pointer_size.bits(),
|
||||||
UintTy::U16 => 16,
|
UintTy::U8 => 8,
|
||||||
UintTy::U32 => 32,
|
UintTy::U16 => 16,
|
||||||
UintTy::U64 => 64,
|
UintTy::U32 => 32,
|
||||||
UintTy::U128 => 128,
|
UintTy::U64 => 64,
|
||||||
|
UintTy::U128 => 128,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
_ => 0,
|
_ => 0,
|
||||||
}
|
}
|
||||||
|
@ -583,14 +593,14 @@ fn span_precision_loss_lint(cx: &LateContext, expr: &Expr, cast_from: Ty, cast_t
|
||||||
}
|
}
|
||||||
|
|
||||||
fn span_lossless_lint(cx: &LateContext, expr: &Expr, op: &Expr, cast_from: Ty, cast_to: Ty) {
|
fn span_lossless_lint(cx: &LateContext, expr: &Expr, op: &Expr, cast_from: Ty, cast_to: Ty) {
|
||||||
span_lint_and_sugg(cx,
|
span_lint_and_sugg(
|
||||||
CAST_LOSSLESS,
|
cx,
|
||||||
expr.span,
|
CAST_LOSSLESS,
|
||||||
&format!("casting {} to {} may become silently lossy if types change",
|
expr.span,
|
||||||
cast_from,
|
&format!("casting {} to {} may become silently lossy if types change", cast_from, cast_to),
|
||||||
cast_to),
|
"try",
|
||||||
"try",
|
format!("{}::from({})", cast_to, &snippet(cx, op.span, "..")),
|
||||||
format!("{}::from({})", cast_to, &snippet(cx, op.span, "..")));
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ArchSuffix {
|
enum ArchSuffix {
|
||||||
|
@ -680,8 +690,9 @@ fn check_lossless(cx: &LateContext, expr: &Expr, op: &Expr, cast_from: Ty, cast_
|
||||||
let cast_signed_to_unsigned = cast_from.is_signed() && !cast_to.is_signed();
|
let cast_signed_to_unsigned = cast_from.is_signed() && !cast_to.is_signed();
|
||||||
let from_nbits = int_ty_to_nbits(cast_from, cx.tcx);
|
let from_nbits = int_ty_to_nbits(cast_from, cx.tcx);
|
||||||
let to_nbits = int_ty_to_nbits(cast_to, cx.tcx);
|
let to_nbits = int_ty_to_nbits(cast_to, cx.tcx);
|
||||||
if !is_isize_or_usize(cast_from) && !is_isize_or_usize(cast_to) &&
|
if !is_isize_or_usize(cast_from) && !is_isize_or_usize(cast_to) && from_nbits < to_nbits &&
|
||||||
from_nbits < to_nbits && !cast_signed_to_unsigned {
|
!cast_signed_to_unsigned
|
||||||
|
{
|
||||||
span_lossless_lint(cx, expr, op, cast_from, cast_to);
|
span_lossless_lint(cx, expr, op, cast_from, cast_to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -776,7 +787,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if let (&ty::TyFloat(FloatTy::F32), &ty::TyFloat(FloatTy::F64)) =
|
if let (&ty::TyFloat(FloatTy::F32), &ty::TyFloat(FloatTy::F64)) =
|
||||||
(&cast_from.sty, &cast_to.sty) {
|
(&cast_from.sty, &cast_to.sty)
|
||||||
|
{
|
||||||
span_lossless_lint(cx, expr, ex, cast_from, cast_to);
|
span_lossless_lint(cx, expr, ex, cast_from, cast_to);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -1011,7 +1023,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CharLitAsU8 {
|
||||||
/// **Known problems:** For `usize` the size of the current compile target will
|
/// **Known problems:** For `usize` the size of the current compile target will
|
||||||
/// be assumed (e.g. 64 bits on 64 bit systems). This means code that uses such
|
/// be assumed (e.g. 64 bits on 64 bit systems). This means code that uses such
|
||||||
/// a comparison to detect target pointer width will trigger this lint. One can
|
/// a comparison to detect target pointer width will trigger this lint. One can
|
||||||
/// use `mem::sizeof` and compare its value or conditional compilation attributes
|
/// use `mem::sizeof` and compare its value or conditional compilation
|
||||||
|
/// attributes
|
||||||
/// like `#[cfg(target_pointer_width = "64")] ..` instead.
|
/// like `#[cfg(target_pointer_width = "64")] ..` instead.
|
||||||
///
|
///
|
||||||
/// **Example:**
|
/// **Example:**
|
||||||
|
@ -1209,7 +1222,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AbsurdExtremeComparisons {
|
||||||
/// will mistakenly imply that it is possible for `x` to be outside the range of
|
/// will mistakenly imply that it is possible for `x` to be outside the range of
|
||||||
/// `u8`.
|
/// `u8`.
|
||||||
///
|
///
|
||||||
/// **Known problems:** https://github.com/rust-lang-nursery/rust-clippy/issues/886
|
/// **Known problems:**
|
||||||
|
/// https://github.com/rust-lang-nursery/rust-clippy/issues/886
|
||||||
///
|
///
|
||||||
/// **Example:**
|
/// **Example:**
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
@ -1290,9 +1304,18 @@ fn numeric_cast_precast_bounds<'a>(cx: &LateContext, expr: &'a Expr) -> Option<(
|
||||||
ty::TyInt(int_ty) => {
|
ty::TyInt(int_ty) => {
|
||||||
Some(match int_ty {
|
Some(match int_ty {
|
||||||
IntTy::I8 => (FullInt::S(i128::from(i8::min_value())), FullInt::S(i128::from(i8::max_value()))),
|
IntTy::I8 => (FullInt::S(i128::from(i8::min_value())), FullInt::S(i128::from(i8::max_value()))),
|
||||||
IntTy::I16 => (FullInt::S(i128::from(i16::min_value())), FullInt::S(i128::from(i16::max_value()))),
|
IntTy::I16 => (
|
||||||
IntTy::I32 => (FullInt::S(i128::from(i32::min_value())), FullInt::S(i128::from(i32::max_value()))),
|
FullInt::S(i128::from(i16::min_value())),
|
||||||
IntTy::I64 => (FullInt::S(i128::from(i64::min_value())), FullInt::S(i128::from(i64::max_value()))),
|
FullInt::S(i128::from(i16::max_value())),
|
||||||
|
),
|
||||||
|
IntTy::I32 => (
|
||||||
|
FullInt::S(i128::from(i32::min_value())),
|
||||||
|
FullInt::S(i128::from(i32::max_value())),
|
||||||
|
),
|
||||||
|
IntTy::I64 => (
|
||||||
|
FullInt::S(i128::from(i64::min_value())),
|
||||||
|
FullInt::S(i128::from(i64::max_value())),
|
||||||
|
),
|
||||||
IntTy::I128 => (FullInt::S(i128::min_value() as i128), FullInt::S(i128::max_value() as i128)),
|
IntTy::I128 => (FullInt::S(i128::min_value() as i128), FullInt::S(i128::max_value() as i128)),
|
||||||
IntTy::Is => (FullInt::S(isize::min_value() as i128), FullInt::S(isize::max_value() as i128)),
|
IntTy::Is => (FullInt::S(isize::min_value() as i128), FullInt::S(isize::max_value() as i128)),
|
||||||
})
|
})
|
||||||
|
@ -1300,9 +1323,18 @@ fn numeric_cast_precast_bounds<'a>(cx: &LateContext, expr: &'a Expr) -> Option<(
|
||||||
ty::TyUint(uint_ty) => {
|
ty::TyUint(uint_ty) => {
|
||||||
Some(match uint_ty {
|
Some(match uint_ty {
|
||||||
UintTy::U8 => (FullInt::U(u128::from(u8::min_value())), FullInt::U(u128::from(u8::max_value()))),
|
UintTy::U8 => (FullInt::U(u128::from(u8::min_value())), FullInt::U(u128::from(u8::max_value()))),
|
||||||
UintTy::U16 => (FullInt::U(u128::from(u16::min_value())), FullInt::U(u128::from(u16::max_value()))),
|
UintTy::U16 => (
|
||||||
UintTy::U32 => (FullInt::U(u128::from(u32::min_value())), FullInt::U(u128::from(u32::max_value()))),
|
FullInt::U(u128::from(u16::min_value())),
|
||||||
UintTy::U64 => (FullInt::U(u128::from(u64::min_value())), FullInt::U(u128::from(u64::max_value()))),
|
FullInt::U(u128::from(u16::max_value())),
|
||||||
|
),
|
||||||
|
UintTy::U32 => (
|
||||||
|
FullInt::U(u128::from(u32::min_value())),
|
||||||
|
FullInt::U(u128::from(u32::max_value())),
|
||||||
|
),
|
||||||
|
UintTy::U64 => (
|
||||||
|
FullInt::U(u128::from(u64::min_value())),
|
||||||
|
FullInt::U(u128::from(u64::max_value())),
|
||||||
|
),
|
||||||
UintTy::U128 => (FullInt::U(u128::min_value() as u128), FullInt::U(u128::max_value() as u128)),
|
UintTy::U128 => (FullInt::U(u128::min_value() as u128), FullInt::U(u128::max_value() as u128)),
|
||||||
UintTy::Us => (FullInt::U(usize::min_value() as u128), FullInt::U(usize::max_value() as u128)),
|
UintTy::Us => (FullInt::U(usize::min_value() as u128), FullInt::U(usize::max_value() as u128)),
|
||||||
})
|
})
|
||||||
|
|
|
@ -8,7 +8,8 @@ use syntax_pos::symbol::keywords::SelfType;
|
||||||
/// **What it does:** Checks for unnecessary repetition of structure name when a
|
/// **What it does:** Checks for unnecessary repetition of structure name when a
|
||||||
/// replacement with `Self` is applicable.
|
/// replacement with `Self` is applicable.
|
||||||
///
|
///
|
||||||
/// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct name
|
/// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct
|
||||||
|
/// name
|
||||||
/// feels inconsistent.
|
/// feels inconsistent.
|
||||||
///
|
///
|
||||||
/// **Known problems:** None.
|
/// **Known problems:** None.
|
||||||
|
@ -78,11 +79,7 @@ struct UseSelfVisitor<'a, 'tcx: 'a> {
|
||||||
|
|
||||||
impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
|
impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
|
||||||
fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
|
fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
|
||||||
if self.item_path.def == path.def &&
|
if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).name != SelfType.name() {
|
||||||
path.segments
|
|
||||||
.last()
|
|
||||||
.expect(SEGMENTS_MSG)
|
|
||||||
.name != SelfType.name() {
|
|
||||||
span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
|
span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
|
||||||
db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
|
db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
|
||||||
});
|
});
|
||||||
|
|
|
@ -92,7 +92,9 @@ pub fn range(expr: &hir::Expr) -> Option<Range> {
|
||||||
end: get_field("end", fields),
|
end: get_field("end", fields),
|
||||||
limits: ast::RangeLimits::HalfOpen,
|
limits: ast::RangeLimits::HalfOpen,
|
||||||
})
|
})
|
||||||
} else if match_qpath(path, &paths::RANGE_TO_INCLUSIVE_STD) || match_qpath(path, &paths::RANGE_TO_INCLUSIVE) {
|
} else if match_qpath(path, &paths::RANGE_TO_INCLUSIVE_STD) ||
|
||||||
|
match_qpath(path, &paths::RANGE_TO_INCLUSIVE)
|
||||||
|
{
|
||||||
Some(Range {
|
Some(Range {
|
||||||
start: None,
|
start: None,
|
||||||
end: get_field("end", fields),
|
end: get_field("end", fields),
|
||||||
|
|
|
@ -196,12 +196,16 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
|
||||||
|
|
||||||
fn eq_path_parameters(&self, left: &PathParameters, right: &PathParameters) -> bool {
|
fn eq_path_parameters(&self, left: &PathParameters, right: &PathParameters) -> bool {
|
||||||
if !(left.parenthesized || right.parenthesized) {
|
if !(left.parenthesized || right.parenthesized) {
|
||||||
over(&left.lifetimes, &right.lifetimes, |l, r| self.eq_lifetime(l, r)) &&
|
over(&left.lifetimes, &right.lifetimes, |l, r| self.eq_lifetime(l, r)) &&
|
||||||
over(&left.types, &right.types, |l, r| self.eq_ty(l, r)) &&
|
over(&left.types, &right.types, |l, r| self.eq_ty(l, r)) &&
|
||||||
over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
|
over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
|
||||||
} else if left.parenthesized && right.parenthesized {
|
} else if left.parenthesized && right.parenthesized {
|
||||||
over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r)) &&
|
over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r)) &&
|
||||||
both(&Some(&left.bindings[0].ty), &Some(&right.bindings[0].ty), |l, r| self.eq_ty(l, r))
|
both(
|
||||||
|
&Some(&left.bindings[0].ty),
|
||||||
|
&Some(&right.bindings[0].ty),
|
||||||
|
|l, r| self.eq_ty(l, r),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
|
@ -252,7 +252,7 @@ fn print_expr(cx: &LateContext, expr: &hir::Expr, indent: usize) {
|
||||||
hir::ExprYield(ref sub) => {
|
hir::ExprYield(ref sub) => {
|
||||||
println!("{}Yield", ind);
|
println!("{}Yield", ind);
|
||||||
print_expr(cx, sub, indent + 1);
|
print_expr(cx, sub, indent + 1);
|
||||||
}
|
},
|
||||||
hir::ExprBlock(_) => {
|
hir::ExprBlock(_) => {
|
||||||
println!("{}Block", ind);
|
println!("{}Block", ind);
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue