mirror of
https://github.com/servo/servo.git
synced 2025-07-28 09:40:33 +01:00
break up lints.rs into separate files
This commit is contained in:
parent
0da57abec6
commit
7ac58f202f
9 changed files with 430 additions and 378 deletions
48
components/plugins/lints/str_to_string.rs
Normal file
48
components/plugins/lints/str_to_string.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use syntax::ast;
|
||||
use rustc::lint::{Context, LintPass, LintArray};
|
||||
use rustc::middle::ty::expr_ty;
|
||||
use rustc::middle::ty;
|
||||
use rustc::middle::typeck::astconv::AstConv;
|
||||
|
||||
declare_lint!(STR_TO_STRING, Deny,
|
||||
"Warn when a String could use into_string() instead of to_string()")
|
||||
|
||||
/// Prefer str.into_string() over str.to_string()
|
||||
///
|
||||
/// The latter creates a `Formatter` and is 5x slower than the former
|
||||
pub struct StrToStringPass;
|
||||
|
||||
impl LintPass for StrToStringPass {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(STR_TO_STRING)
|
||||
}
|
||||
|
||||
fn check_expr(&mut self, cx: &Context, expr: &ast::Expr) {
|
||||
match expr.node {
|
||||
ast::ExprMethodCall(ref method, _, ref args)
|
||||
if method.node.as_str() == "to_string"
|
||||
&& is_str(cx, &*args[0]) => {
|
||||
cx.span_lint(STR_TO_STRING, expr.span,
|
||||
"str.into_string() is more efficient than str.to_string(), please use it instead");
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
|
||||
fn is_str(cx: &Context, expr: &ast::Expr) -> bool {
|
||||
fn walk_ty<'t>(ty: ty::t) -> ty::t {
|
||||
match ty::get(ty).sty {
|
||||
ty::ty_ptr(ref tm) | ty::ty_rptr(_, ref tm) => walk_ty(tm.ty),
|
||||
_ => ty
|
||||
}
|
||||
}
|
||||
match ty::get(walk_ty(expr_ty(cx.tcx, expr))).sty {
|
||||
ty::ty_str => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue