forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#10197 - blyxyas:impl_trait_param, r=Jarcho
Add `impl_trait_in_params` lint As this is a lint about style, and using `impl Trait` is purely cosmetical (even with downsides), a lot of unrelated files needed to allow this lint. --- Resolves rust-lang#10030 changelog: New lint: [`impl_trait_in_params`] [10197](rust-lang/rust-clippy#10197) <!-- changelog_checked -->
- Loading branch information
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_then, is_in_test_function}; | ||
|
||
use rustc_hir::{intravisit::FnKind, Body, HirId}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::Span; | ||
|
||
use super::IMPL_TRAIT_IN_PARAMS; | ||
|
||
pub(super) fn check_fn<'tcx>(cx: &LateContext<'_>, kind: &'tcx FnKind<'_>, body: &'tcx Body<'_>, hir_id: HirId) { | ||
if cx.tcx.visibility(cx.tcx.hir().body_owner_def_id(body.id())).is_public() && !is_in_test_function(cx.tcx, hir_id) | ||
{ | ||
if let FnKind::ItemFn(ident, generics, _) = kind { | ||
for param in generics.params { | ||
if param.is_impl_trait() { | ||
// No generics with nested generics, and no generics like FnMut(x) | ||
span_lint_and_then( | ||
cx, | ||
IMPL_TRAIT_IN_PARAMS, | ||
param.span, | ||
"'`impl Trait` used as a function parameter'", | ||
|diag| { | ||
if let Some(gen_span) = generics.span_for_param_suggestion() { | ||
diag.span_suggestion_with_style( | ||
gen_span, | ||
"add a type paremeter", | ||
format!(", {{ /* Generic name */ }}: {}", ¶m.name.ident().as_str()[5..]), | ||
rustc_errors::Applicability::HasPlaceholders, | ||
rustc_errors::SuggestionStyle::ShowAlways, | ||
); | ||
} else { | ||
diag.span_suggestion_with_style( | ||
Span::new( | ||
body.params[0].span.lo() - rustc_span::BytePos(1), | ||
ident.span.hi(), | ||
ident.span.ctxt(), | ||
ident.span.parent(), | ||
), | ||
"add a type paremeter", | ||
format!("<{{ /* Generic name */ }}: {}>", ¶m.name.ident().as_str()[5..]), | ||
rustc_errors::Applicability::HasPlaceholders, | ||
rustc_errors::SuggestionStyle::ShowAlways, | ||
); | ||
} | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::impl_trait_in_params)] | ||
|
||
pub trait Trait {} | ||
pub trait AnotherTrait<T> {} | ||
|
||
// Should warn | ||
pub fn a(_: impl Trait) {} | ||
pub fn c<C: Trait>(_: C, _: impl Trait) {} | ||
fn d(_: impl AnotherTrait<u32>) {} | ||
|
||
// Shouldn't warn | ||
|
||
pub fn b<B: Trait>(_: B) {} | ||
fn e<T: AnotherTrait<u32>>(_: T) {} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error: '`impl Trait` used as a function parameter' | ||
--> $DIR/impl_trait_in_params.rs:8:13 | ||
| | ||
LL | pub fn a(_: impl Trait) {} | ||
| ^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::impl-trait-in-params` implied by `-D warnings` | ||
help: add a type paremeter | ||
| | ||
LL | pub fn a<{ /* Generic name */ }: Trait>(_: impl Trait) {} | ||
| +++++++++++++++++++++++++++++++ | ||
|
||
error: '`impl Trait` used as a function parameter' | ||
--> $DIR/impl_trait_in_params.rs:9:29 | ||
| | ||
LL | pub fn c<C: Trait>(_: C, _: impl Trait) {} | ||
| ^^^^^^^^^^ | ||
| | ||
help: add a type paremeter | ||
| | ||
LL | pub fn c<C: Trait, { /* Generic name */ }: Trait>(_: C, _: impl Trait) {} | ||
| +++++++++++++++++++++++++++++++ | ||
|
||
error: aborting due to 2 previous errors | ||
|