Skip to content

Commit 15e55f5

Browse files
committed
Deprecates 4 lints
Namely STR_TO_STRING, STRING_TO_STRING, UNSTABLE_AS_SLICE and UNSTABLE_AS_MUT_SLICE.
1 parent 68b2f12 commit 15e55f5

File tree

11 files changed

+115
-171
lines changed

11 files changed

+115
-171
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Table of contents:
1414
* [License](#license)
1515

1616
##Lints
17-
There are 138 lints included in this crate:
17+
There are 134 lints included in this crate:
1818

1919
name | default | meaning
2020
---------------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -124,11 +124,9 @@ name
124124
[single_char_pattern](https://github.com/Manishearth/rust-clippy/wiki#single_char_pattern) | warn | using a single-character str where a char could be used, e.g. `_.split("x")`
125125
[single_match](https://github.com/Manishearth/rust-clippy/wiki#single_match) | warn | a match statement with a single nontrivial arm (i.e, where the other arm is `_ => {}`) is used; recommends `if let` instead
126126
[single_match_else](https://github.com/Manishearth/rust-clippy/wiki#single_match_else) | allow | a match statement with a two arms where the second arm's pattern is a wildcard; recommends `if let` instead
127-
[str_to_string](https://github.com/Manishearth/rust-clippy/wiki#str_to_string) | warn | using `to_string()` on a str, which should be `to_owned()`
128127
[string_add](https://github.com/Manishearth/rust-clippy/wiki#string_add) | allow | using `x + ..` where x is a `String`; suggests using `push_str()` instead
129128
[string_add_assign](https://github.com/Manishearth/rust-clippy/wiki#string_add_assign) | allow | using `x = x + ..` where x is a `String`; suggests using `push_str()` instead
130129
[string_lit_as_bytes](https://github.com/Manishearth/rust-clippy/wiki#string_lit_as_bytes) | warn | calling `as_bytes` on a string literal; suggests using a byte string literal instead
131-
[string_to_string](https://github.com/Manishearth/rust-clippy/wiki#string_to_string) | warn | calling `String::to_string` which is inefficient
132130
[suspicious_assignment_formatting](https://github.com/Manishearth/rust-clippy/wiki#suspicious_assignment_formatting) | warn | suspicious formatting of `*=`, `-=` or `!=`
133131
[suspicious_else_formatting](https://github.com/Manishearth/rust-clippy/wiki#suspicious_else_formatting) | warn | suspicious formatting of `else if`
134132
[temporary_assignment](https://github.com/Manishearth/rust-clippy/wiki#temporary_assignment) | warn | assignments to temporaries
@@ -140,8 +138,6 @@ name
140138
[unit_cmp](https://github.com/Manishearth/rust-clippy/wiki#unit_cmp) | warn | comparing unit values (which is always `true` or `false`, respectively)
141139
[unnecessary_mut_passed](https://github.com/Manishearth/rust-clippy/wiki#unnecessary_mut_passed) | warn | an argument is passed as a mutable reference although the function/method only demands an immutable reference
142140
[unneeded_field_pattern](https://github.com/Manishearth/rust-clippy/wiki#unneeded_field_pattern) | warn | Struct fields are bound to a wildcard instead of using `..`
143-
[unstable_as_mut_slice](https://github.com/Manishearth/rust-clippy/wiki#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
144-
[unstable_as_slice](https://github.com/Manishearth/rust-clippy/wiki#unstable_as_slice) | warn | as_slice is not stable and can be replaced by & v[..]see https://github.com/rust-lang/rust/issues/27729
145141
[unused_collect](https://github.com/Manishearth/rust-clippy/wiki#unused_collect) | warn | `collect()`ing an iterator without using the result; this is usually better written as a for loop
146142
[unused_label](https://github.com/Manishearth/rust-clippy/wiki#unused_label) | warn | unused label
147143
[unused_lifetimes](https://github.com/Manishearth/rust-clippy/wiki#unused_lifetimes) | warn | unused lifetimes in function definitions

src/deprecated_lints.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
macro_rules! declare_deprecated_lint {
2+
(pub $name: ident, $_reason: expr) => {
3+
declare_lint!(pub $name, Allow, "deprecated lint")
4+
}
5+
}
6+
7+
/// **What it does:** Nothing. This lint has been deprecated.
8+
///
9+
/// **Deprecation reason:** This used to check for `Vec::as_slice`, which was unstable with good
10+
/// stable alternatives. `Vec::as_slice` has now been stabilized.
11+
declare_deprecated_lint! {
12+
pub UNSTABLE_AS_SLICE,
13+
"`Vec::as_slice` has been stabilized in 1.7"
14+
}
15+
16+
17+
/// **What it does:** Nothing. This lint has been deprecated.
18+
///
19+
/// **Deprecation reason:** This used to check for `Vec::as_mut_slice`, which was unstable with good
20+
/// stable alternatives. `Vec::as_mut_slice` has now been stabilized.
21+
declare_deprecated_lint! {
22+
pub UNSTABLE_AS_MUT_SLICE,
23+
"`Vec::as_mut_slice` has been stabilized in 1.7"
24+
}
25+
26+
/// **What it does:** Nothing. This lint has been deprecated.
27+
///
28+
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
29+
/// of type `&str`. This is not unidiomatic and with specialization coming, `to_string` could be
30+
/// specialized to be as efficient as `to_owned`.
31+
declare_deprecated_lint! {
32+
pub STR_TO_STRING,
33+
"using `str::to_string` is common even today and specialization will likely happen soon"
34+
}
35+
36+
/// **What it does:** Nothing. This lint has been deprecated.
37+
///
38+
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
39+
/// of type `String`. This is not unidiomatic and with specialization coming, `to_string` could be
40+
/// specialized to be as efficient as `clone`.
41+
declare_deprecated_lint! {
42+
pub STRING_TO_STRING,
43+
"using `string::to_string` is common even today and specialization will likely happen soon"
44+
}

src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ pub mod mut_mut;
8181
pub mod mut_reference;
8282
pub mod mutex_atomic;
8383
pub mod needless_bool;
84-
pub mod needless_features;
8584
pub mod needless_update;
8685
pub mod new_without_default;
8786
pub mod no_effect;
@@ -141,6 +140,13 @@ pub fn plugin_registrar(reg: &mut Registry) {
141140
}
142141
};
143142

143+
let mut store = reg.sess.lint_store.borrow_mut();
144+
store.register_removed("unstable_as_slice", "`Vec::as_slice` has been stabilized in 1.7");
145+
store.register_removed("unstable_as_mut_slice", "`Vec::as_mut_slice` has been stabilized in 1.7");
146+
store.register_removed("str_to_string", "using `str::to_string` is common even today and specialization will likely happen soon");
147+
store.register_removed("string_to_string", "using `string::to_string` is common even today and specialization will likely happen soon");
148+
// end deprecated lints, do not remove this comment, it’s used in `update_lints`
149+
144150
reg.register_late_lint_pass(box types::TypePass);
145151
reg.register_late_lint_pass(box misc::TopLevelRefPass);
146152
reg.register_late_lint_pass(box misc::CmpNan);
@@ -185,7 +191,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
185191
reg.register_late_lint_pass(box open_options::NonSensicalOpenOptions);
186192
reg.register_late_lint_pass(box zero_div_zero::ZeroDivZeroPass);
187193
reg.register_late_lint_pass(box mutex_atomic::MutexAtomic);
188-
reg.register_late_lint_pass(box needless_features::NeedlessFeaturesPass);
189194
reg.register_late_lint_pass(box needless_update::NeedlessUpdatePass);
190195
reg.register_late_lint_pass(box no_effect::NoEffectPass);
191196
reg.register_late_lint_pass(box map_clone::MapClonePass);
@@ -308,8 +313,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
308313
methods::SEARCH_IS_SOME,
309314
methods::SHOULD_IMPLEMENT_TRAIT,
310315
methods::SINGLE_CHAR_PATTERN,
311-
methods::STR_TO_STRING,
312-
methods::STRING_TO_STRING,
313316
methods::WRONG_SELF_CONVENTION,
314317
minmax::MIN_MAX,
315318
misc::CMP_NAN,
@@ -326,8 +329,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
326329
mutex_atomic::MUTEX_ATOMIC,
327330
needless_bool::BOOL_COMPARISON,
328331
needless_bool::NEEDLESS_BOOL,
329-
needless_features::UNSTABLE_AS_MUT_SLICE,
330-
needless_features::UNSTABLE_AS_SLICE,
331332
needless_update::NEEDLESS_UPDATE,
332333
new_without_default::NEW_WITHOUT_DEFAULT,
333334
no_effect::NO_EFFECT,

src/methods.rs

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use rustc::middle::subst::{Subst, TypeSpace};
66
use rustc::middle::ty;
77
use rustc_front::hir::*;
88
use std::borrow::Cow;
9-
use std::{fmt, iter};
9+
use std::fmt;
1010
use syntax::codemap::Span;
1111
use syntax::ptr::P;
1212
use utils::{get_trait_def_id, implements_trait, in_external_macro, in_macro, match_path, match_trait_method,
1313
match_type, method_chain_args, return_ty, same_tys, snippet, snippet_opt, span_lint,
1414
span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth};
15-
use utils::{BTREEMAP_ENTRY_PATH, DEFAULT_TRAIT_PATH, HASHMAP_ENTRY_PATH, OPTION_PATH, RESULT_PATH, STRING_PATH,
15+
use utils::{BTREEMAP_ENTRY_PATH, DEFAULT_TRAIT_PATH, HASHMAP_ENTRY_PATH, OPTION_PATH, RESULT_PATH,
1616
VEC_PATH};
1717
use utils::MethodArgs;
1818

@@ -45,31 +45,6 @@ declare_lint! {
4545
"using `Result.unwrap()`, which might be better handled"
4646
}
4747

48-
/// **What it does:** This lint checks for `.to_string()` method calls on values of type `&str`.
49-
///
50-
/// **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.
51-
///
52-
/// **Known problems:** None
53-
///
54-
/// **Example:** `s.to_string()` where `s: &str`
55-
declare_lint! {
56-
pub STR_TO_STRING, Warn,
57-
"using `to_string()` on a str, which should be `to_owned()`"
58-
}
59-
60-
/// **What it does:** This lint checks for `.to_string()` method calls on values of type `String`.
61-
///
62-
/// **Why is this bad?** This is an non-efficient way to clone a `String`, `.clone()` should be used
63-
/// instead. `String` implements `ToString` mostly for generics.
64-
///
65-
/// **Known problems:** None
66-
///
67-
/// **Example:** `s.to_string()` where `s: String`
68-
declare_lint! {
69-
pub STRING_TO_STRING, Warn,
70-
"calling `String::to_string` which is inefficient"
71-
}
72-
7348
/// **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.
7449
///
7550
/// **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 {
315290
lint_array!(EXTEND_FROM_SLICE,
316291
OPTION_UNWRAP_USED,
317292
RESULT_UNWRAP_USED,
318-
STR_TO_STRING,
319-
STRING_TO_STRING,
320293
SHOULD_IMPLEMENT_TRAIT,
321294
WRONG_SELF_CONVENTION,
322295
WRONG_PUB_SELF_CONVENTION,
@@ -343,8 +316,6 @@ impl LateLintPass for MethodsPass {
343316
// Chain calls
344317
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
345318
lint_unwrap(cx, expr, arglists[0]);
346-
} else if let Some(arglists) = method_chain_args(expr, &["to_string"]) {
347-
lint_to_string(cx, expr, arglists[0]);
348319
} else if let Some(arglists) = method_chain_args(expr, &["ok", "expect"]) {
349320
lint_ok_expect(cx, expr, arglists[0]);
350321
} 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) {
640611
}
641612
}
642613

643-
#[allow(ptr_arg)]
644-
// Type of MethodArgs is potentially a Vec
645-
/// lint use of `to_string()` for `&str`s and `String`s
646-
fn lint_to_string(cx: &LateContext, expr: &Expr, to_string_args: &MethodArgs) {
647-
let (obj_ty, ptr_depth) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&to_string_args[0]));
648-
649-
if obj_ty.sty == ty::TyStr {
650-
let mut arg_str = snippet(cx, to_string_args[0].span, "_");
651-
if ptr_depth > 1 {
652-
arg_str = Cow::Owned(format!("({}{})", iter::repeat('*').take(ptr_depth - 1).collect::<String>(), arg_str));
653-
}
654-
span_lint(cx, STR_TO_STRING, expr.span, &format!("`{}.to_owned()` is faster", arg_str));
655-
} else if match_type(cx, obj_ty, &STRING_PATH) {
656-
span_lint(cx,
657-
STRING_TO_STRING,
658-
expr.span,
659-
"`String::to_string` is an inefficient way to clone a `String`; use `clone()` instead");
660-
}
661-
}
662-
663614
#[allow(ptr_arg)]
664615
// Type of MethodArgs is potentially a Vec
665616
/// lint use of `ok().expect()` for `Result`s

src/needless_features.rs

Lines changed: 0 additions & 68 deletions
This file was deleted.

tests/compile-fail/cmp_owned.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#[deny(cmp_owned)]
55
fn main() {
6-
#[allow(str_to_string)]
76
fn with_to_string(x : &str) {
87
x != "foo".to_string();
98
//~^ ERROR this creates an owned instance just for comparison. Consider using `x != "foo"` to compare without allocation

tests/compile-fail/methods.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,6 @@ fn main() {
296296
let res: Result<i32, ()> = Ok(0);
297297
let _ = res.unwrap(); //~ERROR used unwrap() on a Result
298298

299-
let _ = "str".to_string(); //~ERROR `"str".to_owned()` is faster
300-
301-
let v = &"str";
302-
let string = v.to_string(); //~ERROR `(*v).to_owned()` is faster
303-
let _again = string.to_string(); //~ERROR `String::to_string` is an inefficient way to clone a `String`; use `clone()` instead
304-
305299
res.ok().expect("disaster!"); //~ERROR called `ok().expect()`
306300
// the following should not warn, since `expect` isn't implemented unless
307301
// the error type implements `Debug`

tests/compile-fail/needless_features.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/run-pass/deprecated.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
4+
#[warn(str_to_string)]
5+
//~^WARNING: warning: lint str_to_string has been removed: using `str::to_string`
6+
#[warn(string_to_string)]
7+
//~^WARNING: warning: lint string_to_string has been removed: using `string::to_string`
8+
#[warn(unstable_as_slice)]
9+
//~^WARNING: warning: lint unstable_as_slice has been removed: `Vec::as_slice` has been stabilized
10+
#[warn(unstable_as_mut_slice)]
11+
//~^WARNING: warning: lint unstable_as_mut_slice has been removed: `Vec::as_mut_slice` has been stabilized
12+
fn main() {}

0 commit comments

Comments
 (0)