Deprecates 4 lints

Namely STR_TO_STRING, STRING_TO_STRING, UNSTABLE_AS_SLICE and
UNSTABLE_AS_MUT_SLICE.
This commit is contained in:
mcarton 2016-03-24 19:25:59 +01:00
parent 68b2f12adf
commit 15e55f5df5
11 changed files with 115 additions and 171 deletions

44
src/deprecated_lints.rs Normal file
View file

@ -0,0 +1,44 @@
macro_rules! declare_deprecated_lint {
(pub $name: ident, $_reason: expr) => {
declare_lint!(pub $name, Allow, "deprecated lint")
}
}
/// **What it does:** Nothing. This lint has been deprecated.
///
/// **Deprecation reason:** This used to check for `Vec::as_slice`, which was unstable with good
/// stable alternatives. `Vec::as_slice` has now been stabilized.
declare_deprecated_lint! {
pub UNSTABLE_AS_SLICE,
"`Vec::as_slice` has been stabilized in 1.7"
}
/// **What it does:** Nothing. This lint has been deprecated.
///
/// **Deprecation reason:** This used to check for `Vec::as_mut_slice`, which was unstable with good
/// stable alternatives. `Vec::as_mut_slice` has now been stabilized.
declare_deprecated_lint! {
pub UNSTABLE_AS_MUT_SLICE,
"`Vec::as_mut_slice` has been stabilized in 1.7"
}
/// **What it does:** Nothing. This lint has been deprecated.
///
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
/// of type `&str`. This is not unidiomatic and with specialization coming, `to_string` could be
/// specialized to be as efficient as `to_owned`.
declare_deprecated_lint! {
pub STR_TO_STRING,
"using `str::to_string` is common even today and specialization will likely happen soon"
}
/// **What it does:** Nothing. This lint has been deprecated.
///
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
/// of type `String`. This is not unidiomatic and with specialization coming, `to_string` could be
/// specialized to be as efficient as `clone`.
declare_deprecated_lint! {
pub STRING_TO_STRING,
"using `string::to_string` is common even today and specialization will likely happen soon"
}

View file

@ -81,7 +81,6 @@ pub mod mut_mut;
pub mod mut_reference;
pub mod mutex_atomic;
pub mod needless_bool;
pub mod needless_features;
pub mod needless_update;
pub mod new_without_default;
pub mod no_effect;
@ -141,6 +140,13 @@ pub fn plugin_registrar(reg: &mut Registry) {
}
};
let mut store = reg.sess.lint_store.borrow_mut();
store.register_removed("unstable_as_slice", "`Vec::as_slice` has been stabilized in 1.7");
store.register_removed("unstable_as_mut_slice", "`Vec::as_mut_slice` has been stabilized in 1.7");
store.register_removed("str_to_string", "using `str::to_string` is common even today and specialization will likely happen soon");
store.register_removed("string_to_string", "using `string::to_string` is common even today and specialization will likely happen soon");
// end deprecated lints, do not remove this comment, its used in `update_lints`
reg.register_late_lint_pass(box types::TypePass);
reg.register_late_lint_pass(box misc::TopLevelRefPass);
reg.register_late_lint_pass(box misc::CmpNan);
@ -185,7 +191,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_late_lint_pass(box open_options::NonSensicalOpenOptions);
reg.register_late_lint_pass(box zero_div_zero::ZeroDivZeroPass);
reg.register_late_lint_pass(box mutex_atomic::MutexAtomic);
reg.register_late_lint_pass(box needless_features::NeedlessFeaturesPass);
reg.register_late_lint_pass(box needless_update::NeedlessUpdatePass);
reg.register_late_lint_pass(box no_effect::NoEffectPass);
reg.register_late_lint_pass(box map_clone::MapClonePass);
@ -308,8 +313,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
methods::SEARCH_IS_SOME,
methods::SHOULD_IMPLEMENT_TRAIT,
methods::SINGLE_CHAR_PATTERN,
methods::STR_TO_STRING,
methods::STRING_TO_STRING,
methods::WRONG_SELF_CONVENTION,
minmax::MIN_MAX,
misc::CMP_NAN,
@ -326,8 +329,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
mutex_atomic::MUTEX_ATOMIC,
needless_bool::BOOL_COMPARISON,
needless_bool::NEEDLESS_BOOL,
needless_features::UNSTABLE_AS_MUT_SLICE,
needless_features::UNSTABLE_AS_SLICE,
needless_update::NEEDLESS_UPDATE,
new_without_default::NEW_WITHOUT_DEFAULT,
no_effect::NO_EFFECT,

View file

@ -6,13 +6,13 @@ use rustc::middle::subst::{Subst, TypeSpace};
use rustc::middle::ty;
use rustc_front::hir::*;
use std::borrow::Cow;
use std::{fmt, iter};
use std::fmt;
use syntax::codemap::Span;
use syntax::ptr::P;
use utils::{get_trait_def_id, implements_trait, in_external_macro, in_macro, match_path, match_trait_method,
match_type, method_chain_args, return_ty, same_tys, snippet, snippet_opt, span_lint,
span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth};
use utils::{BTREEMAP_ENTRY_PATH, DEFAULT_TRAIT_PATH, HASHMAP_ENTRY_PATH, OPTION_PATH, RESULT_PATH, STRING_PATH,
use utils::{BTREEMAP_ENTRY_PATH, DEFAULT_TRAIT_PATH, HASHMAP_ENTRY_PATH, OPTION_PATH, RESULT_PATH,
VEC_PATH};
use utils::MethodArgs;
@ -45,31 +45,6 @@ declare_lint! {
"using `Result.unwrap()`, which might be better handled"
}
/// **What it does:** This lint checks for `.to_string()` method calls on values of type `&str`.
///
/// **Why is this bad?** This uses the whole formatting machinery just to clone a string. Using `.to_owned()` is lighter on resources. You can also consider using a [`Cow<'a, str>`](http://doc.rust-lang.org/std/borrow/enum.Cow.html) instead in some cases.
///
/// **Known problems:** None
///
/// **Example:** `s.to_string()` where `s: &str`
declare_lint! {
pub STR_TO_STRING, Warn,
"using `to_string()` on a str, which should be `to_owned()`"
}
/// **What it does:** This lint checks for `.to_string()` method calls on values of type `String`.
///
/// **Why is this bad?** This is an non-efficient way to clone a `String`, `.clone()` should be used
/// instead. `String` implements `ToString` mostly for generics.
///
/// **Known problems:** None
///
/// **Example:** `s.to_string()` where `s: String`
declare_lint! {
pub STRING_TO_STRING, Warn,
"calling `String::to_string` which is inefficient"
}
/// **What it does:** This lint checks for methods that should live in a trait implementation of a `std` trait (see [llogiq's blog post](http://llogiq.github.io/2015/07/30/traits.html) for further information) instead of an inherent implementation.
///
/// **Why is this bad?** Implementing the traits improve ergonomics for users of the code, often with very little cost. Also people seeing a `mul(..)` method may expect `*` to work equally, so you should have good reason to disappoint them.
@ -315,8 +290,6 @@ impl LintPass for MethodsPass {
lint_array!(EXTEND_FROM_SLICE,
OPTION_UNWRAP_USED,
RESULT_UNWRAP_USED,
STR_TO_STRING,
STRING_TO_STRING,
SHOULD_IMPLEMENT_TRAIT,
WRONG_SELF_CONVENTION,
WRONG_PUB_SELF_CONVENTION,
@ -343,8 +316,6 @@ impl LateLintPass for MethodsPass {
// Chain calls
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
lint_unwrap(cx, expr, arglists[0]);
} else if let Some(arglists) = method_chain_args(expr, &["to_string"]) {
lint_to_string(cx, expr, arglists[0]);
} else if let Some(arglists) = method_chain_args(expr, &["ok", "expect"]) {
lint_ok_expect(cx, expr, arglists[0]);
} else if let Some(arglists) = method_chain_args(expr, &["map", "unwrap_or"]) {
@ -640,26 +611,6 @@ fn lint_unwrap(cx: &LateContext, expr: &Expr, unwrap_args: &MethodArgs) {
}
}
#[allow(ptr_arg)]
// Type of MethodArgs is potentially a Vec
/// lint use of `to_string()` for `&str`s and `String`s
fn lint_to_string(cx: &LateContext, expr: &Expr, to_string_args: &MethodArgs) {
let (obj_ty, ptr_depth) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&to_string_args[0]));
if obj_ty.sty == ty::TyStr {
let mut arg_str = snippet(cx, to_string_args[0].span, "_");
if ptr_depth > 1 {
arg_str = Cow::Owned(format!("({}{})", iter::repeat('*').take(ptr_depth - 1).collect::<String>(), arg_str));
}
span_lint(cx, STR_TO_STRING, expr.span, &format!("`{}.to_owned()` is faster", arg_str));
} else if match_type(cx, obj_ty, &STRING_PATH) {
span_lint(cx,
STRING_TO_STRING,
expr.span,
"`String::to_string` is an inefficient way to clone a `String`; use `clone()` instead");
}
}
#[allow(ptr_arg)]
// Type of MethodArgs is potentially a Vec
/// lint use of `ok().expect()` for `Result`s

View file

@ -1,68 +0,0 @@
//! Checks for usage of nightly features that have simple stable equivalents
//!
//! This lint is **warn** by default
use rustc::lint::*;
use rustc_front::hir::*;
use utils::span_lint;
use utils;
/// **What it does:** This lint checks for usage of the `as_slice(..)` function, which is unstable.
///
/// **Why is this bad?** Using this function doesn't make your code better, but it will preclude it from building with stable Rust.
///
/// **Known problems:** None.
///
/// **Example:** `x.as_slice(..)`
declare_lint! {
pub UNSTABLE_AS_SLICE,
Warn,
"as_slice is not stable and can be replaced by & v[..]\
see https://github.com/rust-lang/rust/issues/27729"
}
/// **What it does:** This lint checks for usage of the `as_mut_slice(..)` function, which is unstable.
///
/// **Why is this bad?** Using this function doesn't make your code better, but it will preclude it from building with stable Rust.
///
/// **Known problems:** None.
///
/// **Example:** `x.as_mut_slice(..)`
declare_lint! {
pub UNSTABLE_AS_MUT_SLICE,
Warn,
"as_mut_slice is not stable and can be replaced by &mut v[..]\
see https://github.com/rust-lang/rust/issues/27729"
}
#[derive(Copy,Clone)]
pub struct NeedlessFeaturesPass;
impl LintPass for NeedlessFeaturesPass {
fn get_lints(&self) -> LintArray {
lint_array!(UNSTABLE_AS_SLICE, UNSTABLE_AS_MUT_SLICE)
}
}
impl LateLintPass for NeedlessFeaturesPass {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
if let ExprMethodCall(ref name, _, _) = expr.node {
if name.node.as_str() == "as_slice" && check_paths(cx, expr) {
span_lint(cx,
UNSTABLE_AS_SLICE,
expr.span,
"used as_slice() from the 'convert' nightly feature. Use &[..] instead");
}
if name.node.as_str() == "as_mut_slice" && check_paths(cx, expr) {
span_lint(cx,
UNSTABLE_AS_MUT_SLICE,
expr.span,
"used as_mut_slice() from the 'convert' nightly feature. Use &mut [..] instead");
}
}
}
}
fn check_paths(cx: &LateContext, expr: &Expr) -> bool {
utils::match_impl_method(cx, expr, &["collections", "vec", "Vec<T>"])
}