Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 2 pull requests #4463

Merged
merged 4 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_lints/src/inherent_to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InherentToString {
if impl_item.ident.name.as_str() == "to_string";
let decl = &signature.decl;
if decl.implicit_self.has_implicit_self();
if decl.inputs.len() == 1;

// Check if return type is String
if match_type(cx, return_ty(cx, impl_item.hir_id), &paths::STRING);
Expand Down
2 changes: 2 additions & 0 deletions doc/adding_lints.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ test. That allows us to check if the output is turning into what we want.

Once we are satisfied with the output, we need to run
`tests/ui/update-all-references.sh` to update the `.stderr` file for our lint.
Please note that, we should run `TESTNAME=ui/foo_functions cargo uitest`
every time before running `tests/ui/update-all-references.sh`.
Running `TESTNAME=ui/foo_functions cargo uitest` should pass then. When we
commit our lint, we need to commit the generated `.stderr` files, too.

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/inherent_to_string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![warn(clippy::inherent_to_string)]
#![deny(clippy::inherent_to_string_shadow_display)]
#![allow(clippy::many_single_char_names)]

use std::fmt;

Expand All @@ -12,6 +13,7 @@ struct B;
struct C;
struct D;
struct E;
struct F;

impl A {
// Should be detected; emit warning
Expand Down Expand Up @@ -64,6 +66,13 @@ impl E {
}
}

impl F {
// Should not be detected, as it does not match the function signature
fn to_string(&self, _i: i32) -> String {
"F.to_string()".to_string()
}
}

fn main() {
let a = A;
a.to_string();
Expand All @@ -81,4 +90,7 @@ fn main() {
d.to_string();

E::to_string();

let f = F;
f.to_string(1);
}
4 changes: 2 additions & 2 deletions tests/ui/inherent_to_string.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: implementation of inherent method `to_string(&self) -> String` for type `A`
--> $DIR/inherent_to_string.rs:18:5
--> $DIR/inherent_to_string.rs:20:5
|
LL | / fn to_string(&self) -> String {
LL | | "A.to_string()".to_string()
Expand All @@ -10,7 +10,7 @@ LL | | }
= help: implement trait `Display` for type `A` instead

error: type `C` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`
--> $DIR/inherent_to_string.rs:42:5
--> $DIR/inherent_to_string.rs:44:5
|
LL | / fn to_string(&self) -> String {
LL | | "C.to_string()".to_string()
Expand Down