mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-04 11:54:34 +00:00
Merge pull request #235 from birkenfeld/fix
all: remove unneeded deref and/or ref operations
This commit is contained in:
commit
53d72faca4
@ -71,14 +71,14 @@ fn is_relevant_block(block: &Block) -> bool {
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
block.expr.as_ref().map_or(false, |e| is_relevant_expr(&*e))
|
||||
block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
|
||||
}
|
||||
|
||||
fn is_relevant_expr(expr: &Expr) -> bool {
|
||||
match expr.node {
|
||||
ExprBlock(ref block) => is_relevant_block(block),
|
||||
ExprRet(Some(ref e)) | ExprParen(ref e) =>
|
||||
is_relevant_expr(&*e),
|
||||
is_relevant_expr(e),
|
||||
ExprRet(None) | ExprBreak(_) | ExprMac(_) => false,
|
||||
ExprCall(ref path_expr, _) => {
|
||||
if let ExprPath(_, ref path) = path_expr.node {
|
||||
|
@ -79,7 +79,7 @@ fn single_stmt_of_block(block: &Block) -> Option<&Expr> {
|
||||
} else { None }
|
||||
} else {
|
||||
if block.stmts.is_empty() {
|
||||
if let Some(ref p) = block.expr { Some(&*p) } else { None }
|
||||
if let Some(ref p) = block.expr { Some(p) } else { None }
|
||||
} else { None }
|
||||
}
|
||||
}
|
||||
|
@ -222,7 +222,7 @@ fn neg_float_str(s: String) -> String {
|
||||
if s.starts_with('-') {
|
||||
s[1..].to_owned()
|
||||
} else {
|
||||
format!("-{}", &*s)
|
||||
format!("-{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
@ -299,7 +299,7 @@ impl<'c, 'cc> ConstEvalContext<'c, 'cc> {
|
||||
ExprPath(_, _) => self.fetch_path(e),
|
||||
ExprBlock(ref block) => self.block(block),
|
||||
ExprIf(ref cond, ref then, ref otherwise) =>
|
||||
self.ifthenelse(&*cond, &*then, &*otherwise),
|
||||
self.ifthenelse(cond, then, otherwise),
|
||||
ExprLit(ref lit) => Some(lit_to_constant(&lit.node)),
|
||||
ExprVec(ref vec) => self.multi(vec).map(ConstantVec),
|
||||
ExprTup(ref tup) => self.multi(tup).map(ConstantTuple),
|
||||
@ -362,7 +362,7 @@ impl<'c, 'cc> ConstEvalContext<'c, 'cc> {
|
||||
if b {
|
||||
self.block(then)
|
||||
} else {
|
||||
otherwise.as_ref().and_then(|ref expr| self.expr(expr))
|
||||
otherwise.as_ref().and_then(|expr| self.expr(expr))
|
||||
}
|
||||
} else { None }
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ impl LintPass for EtaPass {
|
||||
ExprCall(_, ref args) |
|
||||
ExprMethodCall(_, _, ref args) => {
|
||||
for arg in args {
|
||||
check_closure(cx, &*arg)
|
||||
check_closure(cx, arg)
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
|
@ -102,7 +102,7 @@ fn check_len_zero(cx: &Context, span: Span, method: &SpannedIdent,
|
||||
args: &[P<Expr>], lit: &Lit, op: &str) {
|
||||
if let Spanned{node: LitInt(0, _), ..} = *lit {
|
||||
if method.node.name == "len" && args.len() == 1 &&
|
||||
has_is_empty(cx, &*args[0]) {
|
||||
has_is_empty(cx, &args[0]) {
|
||||
span_lint(cx, LEN_ZERO, span, &format!(
|
||||
"consider replacing the len comparison with `{}{}.is_empty()`",
|
||||
op, snippet(cx, args[0].span, "_")))
|
||||
|
@ -26,14 +26,14 @@ impl LintPass for LifetimePass {
|
||||
|
||||
fn check_impl_item(&mut self, cx: &Context, item: &ImplItem) {
|
||||
if let MethodImplItem(ref sig, _) = item.node {
|
||||
check_fn_inner(cx, &*sig.decl, Some(&sig.explicit_self),
|
||||
check_fn_inner(cx, &sig.decl, Some(&sig.explicit_self),
|
||||
&sig.generics.lifetimes, item.span);
|
||||
}
|
||||
}
|
||||
|
||||
fn check_trait_item(&mut self, cx: &Context, item: &TraitItem) {
|
||||
if let MethodTraitItem(ref sig, _) = item.node {
|
||||
check_fn_inner(cx, &*sig.decl, Some(&sig.explicit_self),
|
||||
check_fn_inner(cx, &sig.decl, Some(&sig.explicit_self),
|
||||
&sig.generics.lifetimes, item.span);
|
||||
}
|
||||
}
|
||||
@ -92,7 +92,7 @@ fn could_use_elision(func: &FnDecl, slf: Option<&ExplicitSelf>,
|
||||
}
|
||||
// extract lifetimes in input argument types
|
||||
for arg in &func.inputs {
|
||||
walk_ty(&mut input_visitor, &*arg.ty);
|
||||
walk_ty(&mut input_visitor, &arg.ty);
|
||||
}
|
||||
// extract lifetimes in output type
|
||||
if let Return(ref ty) = func.output {
|
||||
|
@ -95,9 +95,9 @@ fn recover_for_loop(expr: &Expr) -> Option<(&Pat, &Expr, &Expr)> {
|
||||
let PatEnum(_, Some(ref somepats)) = innerarms[0].pats[0].node,
|
||||
somepats.len() == 1
|
||||
], {
|
||||
return Some((&*somepats[0],
|
||||
&*iterargs[0],
|
||||
&*innerarms[0].body));
|
||||
return Some((&somepats[0],
|
||||
&iterargs[0],
|
||||
&innerarms[0].body));
|
||||
}
|
||||
}
|
||||
None
|
||||
|
@ -34,7 +34,7 @@ impl LintPass for MatchPass {
|
||||
// when an enum is extended, so we don't consider these cases
|
||||
arms[1].pats[0].node == PatWild(PatWildSingle) &&
|
||||
// finally, we don't want any content in the second arm (unit or empty block)
|
||||
is_unit_expr(&*arms[1].body)
|
||||
is_unit_expr(&arms[1].body)
|
||||
{
|
||||
let body_code = snippet_block(cx, arms[0].body.span, "..");
|
||||
let body_code = if let ExprBlock(_) = arms[0].body.node {
|
||||
@ -46,10 +46,10 @@ impl LintPass for MatchPass {
|
||||
"you seem to be trying to use match for \
|
||||
destructuring a single pattern. Did you mean to \
|
||||
use `if let`?",
|
||||
&*format!("try\nif let {} = {} {}",
|
||||
snippet(cx, arms[0].pats[0].span, ".."),
|
||||
snippet(cx, ex.span, ".."),
|
||||
body_code)
|
||||
&format!("try\nif let {} = {} {}",
|
||||
snippet(cx, arms[0].pats[0].span, ".."),
|
||||
snippet(cx, ex.span, ".."),
|
||||
body_code)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ impl LintPass for MethodsPass {
|
||||
|
||||
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
|
||||
if let ExprMethodCall(ref ident, _, ref args) = expr.node {
|
||||
let obj_ty = walk_ptrs_ty(cx.tcx.expr_ty(&*args[0]));
|
||||
let obj_ty = walk_ptrs_ty(cx.tcx.expr_ty(&args[0]));
|
||||
if ident.node.name == "unwrap" {
|
||||
if match_type(cx, obj_ty, &OPTION_PATH) {
|
||||
span_lint(cx, OPTION_UNWRAP_USED, expr.span,
|
||||
|
@ -203,7 +203,7 @@ fn check_to_owned(cx: &Context, expr: &Expr, other_span: Span) {
|
||||
|
||||
fn is_str_arg(cx: &Context, args: &[P<Expr>]) -> bool {
|
||||
args.len() == 1 && if let ty::TyStr =
|
||||
walk_ptrs_ty(cx.tcx.expr_ty(&*args[0])).sty { true } else { false }
|
||||
walk_ptrs_ty(cx.tcx.expr_ty(&args[0])).sty { true } else { false }
|
||||
}
|
||||
|
||||
declare_lint!(pub MODULO_ONE, Warn, "taking a number modulo 1, which always returns 0");
|
||||
|
@ -5,7 +5,7 @@
|
||||
use rustc::lint::*;
|
||||
use syntax::ast::*;
|
||||
|
||||
use utils::{de_p, span_lint, snippet};
|
||||
use utils::{span_lint, snippet};
|
||||
|
||||
declare_lint! {
|
||||
pub NEEDLESS_BOOL,
|
||||
@ -55,7 +55,7 @@ impl LintPass for NeedlessBool {
|
||||
|
||||
fn fetch_bool_block(block: &Block) -> Option<bool> {
|
||||
if block.stmts.is_empty() {
|
||||
block.expr.as_ref().map(de_p).and_then(fetch_bool_expr)
|
||||
block.expr.as_ref().and_then(|e| fetch_bool_expr(e))
|
||||
} else { None }
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ impl LintPass for PtrArg {
|
||||
|
||||
fn check_fn(cx: &Context, decl: &FnDecl) {
|
||||
for arg in &decl.inputs {
|
||||
if let Some(pat_ty) = cx.tcx.pat_ty_opt(&*arg.pat) {
|
||||
if let Some(pat_ty) = cx.tcx.pat_ty_opt(&arg.pat) {
|
||||
if let ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = pat_ty.sty {
|
||||
if match_type(cx, ty, &VEC_PATH) {
|
||||
span_lint(cx, PTR_ARG, arg.ty.span,
|
||||
|
@ -50,7 +50,7 @@ impl ReturnPass {
|
||||
// a match expr, check all arms
|
||||
ExprMatch(_, ref arms, _) => {
|
||||
for arm in arms {
|
||||
self.check_final_expr(cx, &*arm.body);
|
||||
self.check_final_expr(cx, &arm.body);
|
||||
}
|
||||
}
|
||||
_ => { }
|
||||
@ -76,7 +76,7 @@ impl ReturnPass {
|
||||
let PatIdent(_, Spanned { node: id, .. }, _) = local.pat.node,
|
||||
let Some(ref retexpr) = block.expr,
|
||||
let ExprPath(_, ref path) = retexpr.node,
|
||||
match_path(path, &[&*id.name.as_str()])
|
||||
match_path(path, &[&id.name.as_str()])
|
||||
], {
|
||||
self.emit_let_lint(cx, retexpr.span, initexpr.span);
|
||||
}
|
||||
|
@ -70,8 +70,8 @@ fn is_add(cx: &Context, src: &Expr, target: &Expr) -> bool {
|
||||
is_exp_equal(cx, target, left),
|
||||
ExprBlock(ref block) => block.stmts.is_empty() &&
|
||||
block.expr.as_ref().map_or(false,
|
||||
|expr| is_add(cx, &*expr, target)),
|
||||
ExprParen(ref expr) => is_add(cx, &*expr, target),
|
||||
|expr| is_add(cx, expr, target)),
|
||||
ExprParen(ref expr) => is_add(cx, expr, target),
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
10
src/types.rs
10
src/types.rs
@ -55,7 +55,7 @@ declare_lint!(pub LET_UNIT_VALUE, Warn,
|
||||
fn check_let_unit(cx: &Context, decl: &Decl, info: Option<&ExpnInfo>) {
|
||||
if in_macro(cx, info) { return; }
|
||||
if let DeclLocal(ref local) = decl.node {
|
||||
let bindtype = &cx.tcx.pat_ty(&*local.pat).sty;
|
||||
let bindtype = &cx.tcx.pat_ty(&local.pat).sty;
|
||||
if *bindtype == ty::TyTuple(vec![]) {
|
||||
span_lint(cx, LET_UNIT_VALUE, decl.span, &format!(
|
||||
"this let-binding has unit value. Consider omitting `let {} =`",
|
||||
@ -210,7 +210,7 @@ impl LintPass for CastPass {
|
||||
|
||||
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
|
||||
if let ExprCast(ref ex, _) = expr.node {
|
||||
let (cast_from, cast_to) = (cx.tcx.expr_ty(&*ex), cx.tcx.expr_ty(expr));
|
||||
let (cast_from, cast_to) = (cx.tcx.expr_ty(ex), cx.tcx.expr_ty(expr));
|
||||
if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx, expr.span) {
|
||||
match (cast_from.is_integral(), cast_to.is_integral()) {
|
||||
(true, false) => {
|
||||
@ -263,14 +263,14 @@ impl LintPass for TypeComplexityPass {
|
||||
}
|
||||
|
||||
fn check_struct_field(&mut self, cx: &Context, field: &StructField) {
|
||||
check_type(cx, &*field.node.ty);
|
||||
check_type(cx, &field.node.ty);
|
||||
}
|
||||
|
||||
fn check_variant(&mut self, cx: &Context, var: &Variant, _: &Generics) {
|
||||
// StructVariant is covered by check_struct_field
|
||||
if let TupleVariantKind(ref args) = var.node.kind {
|
||||
for arg in args {
|
||||
check_type(cx, &*arg.ty);
|
||||
check_type(cx, &arg.ty);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -312,7 +312,7 @@ impl LintPass for TypeComplexityPass {
|
||||
|
||||
fn check_fndecl(cx: &Context, decl: &FnDecl) {
|
||||
for arg in &decl.inputs {
|
||||
check_type(cx, &*arg.ty);
|
||||
check_type(cx, &arg.ty);
|
||||
}
|
||||
if let Return(ref ty) = decl.output {
|
||||
check_type(cx, ty);
|
||||
|
@ -1,7 +1,6 @@
|
||||
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;
|
||||
@ -130,9 +129,6 @@ pub fn get_parent_expr<'c>(cx: &'c Context, e: &Expr) -> Option<&'c Expr> {
|
||||
if let NodeExpr(parent) = node { Some(parent) } else { None } )
|
||||
}
|
||||
|
||||
/// dereference a P<T> and return a ref on the result
|
||||
pub fn de_p<T>(p: &P<T>) -> &T { &*p }
|
||||
|
||||
#[cfg(not(feature="structured_logging"))]
|
||||
pub fn span_lint(cx: &Context, lint: &'static Lint, sp: Span, msg: &str) {
|
||||
cx.span_lint(lint, sp, msg);
|
||||
|
Loading…
Reference in New Issue
Block a user