mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-28 09:44:08 +00:00
parent
b749d832cc
commit
15e3774cb4
@ -52,7 +52,7 @@ impl LateLintPass for LenZero {
|
||||
|
||||
fn check_trait_items(cx: &LateContext, item: &Item, trait_items: &[P<TraitItem>]) {
|
||||
fn is_named_self(item: &TraitItem, name: &str) -> bool {
|
||||
item.name == name && if let MethodTraitItem(ref sig, _) =
|
||||
item.name.as_str() == name && if let MethodTraitItem(ref sig, _) =
|
||||
item.node { is_self_sig(sig) } else { false }
|
||||
}
|
||||
|
||||
@ -71,7 +71,7 @@ fn check_trait_items(cx: &LateContext, item: &Item, trait_items: &[P<TraitItem>]
|
||||
|
||||
fn check_impl_items(cx: &LateContext, item: &Item, impl_items: &[P<ImplItem>]) {
|
||||
fn is_named_self(item: &ImplItem, name: &str) -> bool {
|
||||
item.name == name && if let MethodImplItem(ref sig, _) =
|
||||
item.name.as_str() == name && if let MethodImplItem(ref sig, _) =
|
||||
item.node { is_self_sig(sig) } else { false }
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@ fn is_self_sig(sig: &MethodSig) -> bool {
|
||||
fn check_cmp(cx: &LateContext, span: Span, left: &Expr, right: &Expr, op: &str) {
|
||||
// check if we are in an is_empty() method
|
||||
if let Some(name) = get_item_name(cx, left) {
|
||||
if name == "is_empty" { return; }
|
||||
if name.as_str() == "is_empty" { return; }
|
||||
}
|
||||
match (&left.node, &right.node) {
|
||||
(&ExprLit(ref lit), &ExprMethodCall(ref method, _, ref args)) =>
|
||||
@ -112,7 +112,7 @@ fn check_cmp(cx: &LateContext, span: Span, left: &Expr, right: &Expr, op: &str)
|
||||
fn check_len_zero(cx: &LateContext, span: Span, name: &Name,
|
||||
args: &[P<Expr>], lit: &Lit, op: &str) {
|
||||
if let Spanned{node: LitInt(0, _), ..} = *lit {
|
||||
if name == &"len" && args.len() == 1 &&
|
||||
if name.as_str() == "len" && args.len() == 1 &&
|
||||
has_is_empty(cx, &args[0]) {
|
||||
span_lint(cx, LEN_ZERO, span, &format!(
|
||||
"consider replacing the len comparison with `{}{}.is_empty()`",
|
||||
@ -128,7 +128,7 @@ fn has_is_empty(cx: &LateContext, expr: &Expr) -> bool {
|
||||
if let &MethodTraitItemId(def_id) = id {
|
||||
if let ty::MethodTraitItem(ref method) =
|
||||
cx.tcx.impl_or_trait_item(def_id) {
|
||||
method.name == "is_empty"
|
||||
method.name.as_str() == "is_empty"
|
||||
&& method.fty.sig.skip_binder().inputs.len() == 1
|
||||
} else { false }
|
||||
} else { false }
|
||||
|
@ -161,7 +161,7 @@ struct RefVisitor(Vec<RefLt>);
|
||||
impl RefVisitor {
|
||||
fn record(&mut self, lifetime: &Option<Lifetime>) {
|
||||
if let &Some(ref lt) = lifetime {
|
||||
if lt.name == "'static" {
|
||||
if lt.name.as_str() == "'static" {
|
||||
self.0.push(Static);
|
||||
} else {
|
||||
self.0.push(Named(lt.name));
|
||||
|
@ -110,17 +110,17 @@ impl LateLintPass for LoopsPass {
|
||||
if args.len() == 1 {
|
||||
let method_name = method.node;
|
||||
// check for looping over x.iter() or x.iter_mut(), could use &x or &mut x
|
||||
if method_name == "iter" || method_name == "iter_mut" {
|
||||
if method_name.as_str() == "iter" || method_name.as_str() == "iter_mut" {
|
||||
if is_ref_iterable_type(cx, &args[0]) {
|
||||
let object = snippet(cx, args[0].span, "_");
|
||||
span_lint(cx, EXPLICIT_ITER_LOOP, expr.span, &format!(
|
||||
"it is more idiomatic to loop over `&{}{}` instead of `{}.{}()`",
|
||||
if method_name == "iter_mut" { "mut " } else { "" },
|
||||
if method_name.as_str() == "iter_mut" { "mut " } else { "" },
|
||||
object, object, method_name));
|
||||
}
|
||||
}
|
||||
// check for looping over Iterator::next() which is not what you want
|
||||
else if method_name == "next" &&
|
||||
else if method_name.as_str() == "next" &&
|
||||
match_trait_method(cx, arg, &["core", "iter", "Iterator"]) {
|
||||
span_lint(cx, ITER_NEXT_LOOP, expr.span,
|
||||
"you are iterating over `Iterator::next()` which is an Option; \
|
||||
@ -191,7 +191,7 @@ impl LateLintPass for LoopsPass {
|
||||
fn check_stmt(&mut self, cx: &LateContext, stmt: &Stmt) {
|
||||
if let StmtSemi(ref expr, _) = stmt.node {
|
||||
if let ExprMethodCall(ref method, _, ref args) = expr.node {
|
||||
if args.len() == 1 && method.node == "collect" &&
|
||||
if args.len() == 1 && method.node.as_str() == "collect" &&
|
||||
match_trait_method(cx, expr, &["core", "iter", "Iterator"]) {
|
||||
span_lint(cx, UNUSED_COLLECT, expr.span, &format!(
|
||||
"you are collect()ing an iterator and throwing away the result. \
|
||||
|
@ -42,7 +42,7 @@ impl LateLintPass for MethodsPass {
|
||||
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
||||
if let ExprMethodCall(ref name, _, ref args) = expr.node {
|
||||
let (obj_ty, ptr_depth) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&args[0]));
|
||||
if name.node == "unwrap" {
|
||||
if name.node.as_str() == "unwrap" {
|
||||
if match_type(cx, obj_ty, &OPTION_PATH) {
|
||||
span_lint(cx, OPTION_UNWRAP_USED, expr.span,
|
||||
"used unwrap() on an Option value. If you don't want \
|
||||
@ -54,7 +54,7 @@ impl LateLintPass for MethodsPass {
|
||||
of Err values is preferred");
|
||||
}
|
||||
}
|
||||
else if name.node == "to_string" {
|
||||
else if name.node.as_str() == "to_string" {
|
||||
if obj_ty.sty == ty::TyStr {
|
||||
let mut arg_str = snippet(cx, args[0].span, "_");
|
||||
if ptr_depth > 1 {
|
||||
@ -82,7 +82,7 @@ impl LateLintPass for MethodsPass {
|
||||
for &(method_name, n_args, self_kind, out_type, trait_name) in &TRAIT_METHODS {
|
||||
if_let_chain! {
|
||||
[
|
||||
name == method_name,
|
||||
name.as_str() == method_name,
|
||||
sig.decl.inputs.len() == n_args,
|
||||
out_type.matches(&sig.decl.output),
|
||||
self_kind.matches(&sig.explicit_self.node, false)
|
||||
|
11
src/misc.rs
11
src/misc.rs
@ -94,7 +94,7 @@ impl LateLintPass for CmpNan {
|
||||
}
|
||||
|
||||
fn check_nan(cx: &LateContext, path: &Path, span: Span) {
|
||||
path.segments.last().map(|seg| if seg.identifier.name == "NAN" {
|
||||
path.segments.last().map(|seg| if seg.identifier.name.as_str() == "NAN" {
|
||||
span_lint(cx, CMP_NAN, span,
|
||||
"doomed comparison with NAN, use `std::{f32,f64}::is_nan()` instead");
|
||||
});
|
||||
@ -124,9 +124,10 @@ impl LateLintPass for FloatCmp {
|
||||
return;
|
||||
}
|
||||
if let Some(name) = get_item_name(cx, expr) {
|
||||
let name = name.as_str();
|
||||
if name == "eq" || name == "ne" || name == "is_nan" ||
|
||||
name.as_str().starts_with("eq_") ||
|
||||
name.as_str().ends_with("_eq") {
|
||||
name.starts_with("eq_") ||
|
||||
name.ends_with("_eq") {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -174,8 +175,8 @@ impl LateLintPass for CmpOwned {
|
||||
fn check_to_owned(cx: &LateContext, expr: &Expr, other_span: Span) {
|
||||
match expr.node {
|
||||
ExprMethodCall(Spanned{node: ref name, ..}, _, ref args) => {
|
||||
if name == &"to_string" ||
|
||||
name == &"to_owned" && is_str_arg(cx, args) {
|
||||
if name.as_str() == "to_string" ||
|
||||
name.as_str() == "to_owned" && is_str_arg(cx, args) {
|
||||
span_lint(cx, CMP_OWNED, expr.span, &format!(
|
||||
"this creates an owned instance just for comparison. \
|
||||
Consider using `{}.as_slice()` to compare without allocation",
|
||||
|
@ -22,7 +22,7 @@ impl LateLintPass for StepByZero {
|
||||
if let ExprMethodCall(Spanned { node: ref name, .. }, _,
|
||||
ref args) = expr.node {
|
||||
// Only warn on literal ranges.
|
||||
if name == &"step_by" && args.len() == 2 &&
|
||||
if name.as_str() == "step_by" && args.len() == 2 &&
|
||||
is_range(cx, &args[0]) && is_integer_literal(&args[1], 0) {
|
||||
cx.span_lint(RANGE_STEP_BY_ZERO, expr.span,
|
||||
"Range::step_by(0) produces an infinite iterator. \
|
||||
|
@ -36,7 +36,7 @@ pub fn in_external_macro<T: LintContext>(cx: &T, span: Span) -> bool {
|
||||
opt_info.map_or(false, |info| {
|
||||
match info.callee.format {
|
||||
ExpnFormat::CompilerExpansion(..) => {
|
||||
if info.callee.name() == "closure expansion" {
|
||||
if info.callee.name().as_str() == "closure expansion" {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@ -66,7 +66,8 @@ pub fn in_external_macro<T: LintContext>(cx: &T, span: Span) -> bool {
|
||||
/// usage e.g. with
|
||||
/// `match_def_path(cx, id, &["core", "option", "Option"])`
|
||||
pub fn match_def_path(cx: &LateContext, def_id: DefId, path: &[&str]) -> bool {
|
||||
cx.tcx.with_path(def_id, |iter| iter.zip(path).all(|(nm, p)| nm.name() == p))
|
||||
cx.tcx.with_path(def_id, |iter| iter.zip(path)
|
||||
.all(|(nm, p)| nm.name().as_str() == *p))
|
||||
}
|
||||
|
||||
/// check if type is struct or enum type with given def path
|
||||
@ -98,7 +99,7 @@ pub fn match_trait_method(cx: &LateContext, expr: &Expr, path: &[&str]) -> bool
|
||||
/// `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 == b)
|
||||
|(a, b)| a.identifier.name.as_str() == *b)
|
||||
}
|
||||
|
||||
/// get the name of the item the expression is in, if available
|
||||
|
Loading…
Reference in New Issue
Block a user