Skip to content

Commit d2c9047

Browse files
committed
Auto merge of rust-lang#11205 - Centri3:rust-lang#11201, r=Manishearth
[`inherent_to_string`]: Don't lint `unsafe` or `extern` fns Fixes rust-lang#11201 changelog: [`inherent_to_string`]: No longer lints `unsafe` or `extern` fns
2 parents ee8a429 + f3f7f63 commit d2c9047

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

clippy_lints/src/inherent_to_string.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::{implements_trait, is_type_lang_item};
33
use clippy_utils::{return_ty, trait_ref_of_method};
4-
use if_chain::if_chain;
5-
use rustc_hir::{GenericParamKind, ImplItem, ImplItemKind, LangItem};
4+
use rustc_hir::{GenericParamKind, ImplItem, ImplItemKind, LangItem, Unsafety};
65
use rustc_lint::{LateContext, LateLintPass};
76
use rustc_session::{declare_lint_pass, declare_tool_lint};
87
use rustc_span::sym;
8+
use rustc_target::spec::abi::Abi;
99

1010
declare_clippy_lint! {
1111
/// ### What it does
@@ -95,24 +95,23 @@ impl<'tcx> LateLintPass<'tcx> for InherentToString {
9595
return;
9696
}
9797

98-
if_chain! {
99-
// Check if item is a method, called to_string and has a parameter 'self'
100-
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind;
101-
if impl_item.ident.name == sym::to_string;
102-
let decl = &signature.decl;
103-
if decl.implicit_self.has_implicit_self();
104-
if decl.inputs.len() == 1;
105-
if impl_item.generics.params.iter().all(|p| matches!(p.kind, GenericParamKind::Lifetime { .. }));
106-
98+
// Check if item is a method called `to_string` and has a parameter 'self'
99+
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind
100+
// #11201
101+
&& let header = signature.header
102+
&& header.unsafety == Unsafety::Normal
103+
&& header.abi == Abi::Rust
104+
&& impl_item.ident.name == sym::to_string
105+
&& let decl = signature.decl
106+
&& decl.implicit_self.has_implicit_self()
107+
&& decl.inputs.len() == 1
108+
&& impl_item.generics.params.iter().all(|p| matches!(p.kind, GenericParamKind::Lifetime { .. }))
107109
// Check if return type is String
108-
if is_type_lang_item(cx, return_ty(cx, impl_item.owner_id), LangItem::String);
109-
110+
&& is_type_lang_item(cx, return_ty(cx, impl_item.owner_id), LangItem::String)
110111
// Filters instances of to_string which are required by a trait
111-
if trait_ref_of_method(cx, impl_item.owner_id.def_id).is_none();
112-
113-
then {
114-
show_lint(cx, impl_item);
115-
}
112+
&& trait_ref_of_method(cx, impl_item.owner_id.def_id).is_none()
113+
{
114+
show_lint(cx, impl_item);
116115
}
117116
}
118117
}

tests/ui/inherent_to_string.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#![warn(clippy::inherent_to_string)]
2-
#![deny(clippy::inherent_to_string_shadow_display)]
1+
#![allow(improper_ctypes_definitions)]
32

43
use std::fmt;
54

@@ -14,6 +13,9 @@ struct D;
1413
struct E;
1514
struct F;
1615
struct G;
16+
struct H;
17+
struct I;
18+
struct J;
1719

1820
impl A {
1921
// Should be detected; emit warning
@@ -80,6 +82,26 @@ impl G {
8082
}
8183
}
8284

85+
// Issue #11201
86+
87+
impl H {
88+
unsafe fn to_string(&self) -> String {
89+
"G.to_string()".to_string()
90+
}
91+
}
92+
93+
impl I {
94+
extern "C" fn to_string(&self) -> String {
95+
"G.to_string()".to_string()
96+
}
97+
}
98+
99+
impl J {
100+
unsafe extern "C" fn to_string(&self) -> String {
101+
"G.to_string()".to_string()
102+
}
103+
}
104+
83105
fn main() {
84106
let a = A;
85107
a.to_string();

tests/ui/inherent_to_string.stderr

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: implementation of inherent method `to_string(&self) -> String` for type `A`
2-
--> $DIR/inherent_to_string.rs:20:5
2+
--> $DIR/inherent_to_string.rs:22:5
33
|
44
LL | / fn to_string(&self) -> String {
55
LL | | "A.to_string()".to_string()
@@ -10,19 +10,15 @@ LL | | }
1010
= note: `-D clippy::inherent-to-string` implied by `-D warnings`
1111

1212
error: type `C` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`
13-
--> $DIR/inherent_to_string.rs:44:5
13+
--> $DIR/inherent_to_string.rs:46:5
1414
|
1515
LL | / fn to_string(&self) -> String {
1616
LL | | "C.to_string()".to_string()
1717
LL | | }
1818
| |_____^
1919
|
2020
= help: remove the inherent method from type `C`
21-
note: the lint level is defined here
22-
--> $DIR/inherent_to_string.rs:2:9
23-
|
24-
LL | #![deny(clippy::inherent_to_string_shadow_display)]
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
= note: `#[deny(clippy::inherent_to_string_shadow_display)]` on by default
2622

2723
error: aborting due to 2 previous errors
2824

0 commit comments

Comments
 (0)