Skip to content

[inherent_to_string]: Don't lint unsafe or extern fns #11205

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

Merged
merged 1 commit into from
Jul 21, 2023
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
35 changes: 17 additions & 18 deletions clippy_lints/src/inherent_to_string.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::ty::{implements_trait, is_type_lang_item};
use clippy_utils::{return_ty, trait_ref_of_method};
use if_chain::if_chain;
use rustc_hir::{GenericParamKind, ImplItem, ImplItemKind, LangItem};
use rustc_hir::{GenericParamKind, ImplItem, ImplItemKind, LangItem, Unsafety};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;
use rustc_target::spec::abi::Abi;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -95,24 +95,23 @@ impl<'tcx> LateLintPass<'tcx> for InherentToString {
return;
}

if_chain! {
// Check if item is a method, called to_string and has a parameter 'self'
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind;
if impl_item.ident.name == sym::to_string;
let decl = &signature.decl;
if decl.implicit_self.has_implicit_self();
if decl.inputs.len() == 1;
if impl_item.generics.params.iter().all(|p| matches!(p.kind, GenericParamKind::Lifetime { .. }));

// Check if item is a method called `to_string` and has a parameter 'self'
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind
// #11201
&& let header = signature.header
&& header.unsafety == Unsafety::Normal
&& header.abi == Abi::Rust
&& impl_item.ident.name == sym::to_string
&& let decl = signature.decl
&& decl.implicit_self.has_implicit_self()
&& decl.inputs.len() == 1
&& impl_item.generics.params.iter().all(|p| matches!(p.kind, GenericParamKind::Lifetime { .. }))
// Check if return type is String
if is_type_lang_item(cx, return_ty(cx, impl_item.owner_id), LangItem::String);

&& is_type_lang_item(cx, return_ty(cx, impl_item.owner_id), LangItem::String)
// Filters instances of to_string which are required by a trait
if trait_ref_of_method(cx, impl_item.owner_id.def_id).is_none();

then {
show_lint(cx, impl_item);
}
&& trait_ref_of_method(cx, impl_item.owner_id.def_id).is_none()
{
show_lint(cx, impl_item);
}
}
}
Expand Down
26 changes: 24 additions & 2 deletions tests/ui/inherent_to_string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![warn(clippy::inherent_to_string)]
#![deny(clippy::inherent_to_string_shadow_display)]
#![allow(improper_ctypes_definitions)]

use std::fmt;

Expand All @@ -14,6 +13,9 @@ struct D;
struct E;
struct F;
struct G;
struct H;
struct I;
struct J;

impl A {
// Should be detected; emit warning
Expand Down Expand Up @@ -80,6 +82,26 @@ impl G {
}
}

// Issue #11201

impl H {
unsafe fn to_string(&self) -> String {
"G.to_string()".to_string()
}
}

impl I {
extern "C" fn to_string(&self) -> String {
"G.to_string()".to_string()
}
}

impl J {
unsafe extern "C" fn to_string(&self) -> String {
"G.to_string()".to_string()
}
}

fn main() {
let a = A;
a.to_string();
Expand Down
10 changes: 3 additions & 7 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:20:5
--> $DIR/inherent_to_string.rs:22:5
|
LL | / fn to_string(&self) -> String {
LL | | "A.to_string()".to_string()
Expand All @@ -10,19 +10,15 @@ LL | | }
= note: `-D clippy::inherent-to-string` implied by `-D warnings`

error: type `C` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`
--> $DIR/inherent_to_string.rs:44:5
--> $DIR/inherent_to_string.rs:46:5
|
LL | / fn to_string(&self) -> String {
LL | | "C.to_string()".to_string()
LL | | }
| |_____^
|
= help: remove the inherent method from type `C`
note: the lint level is defined here
--> $DIR/inherent_to_string.rs:2:9
|
LL | #![deny(clippy::inherent_to_string_shadow_display)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `#[deny(clippy::inherent_to_string_shadow_display)]` on by default

error: aborting due to 2 previous errors