Skip to content

Commit 3cb75c2

Browse files
committed
Remove expansion restriction + fix doc and tests naming
1 parent 2a3ee5f commit 3cb75c2

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ declare_clippy_lint! {
725725
/// **Known problems:** None.
726726
///
727727
/// **Example:**
728+
/// In an impl block:
728729
/// ```rust
729730
/// # struct Foo;
730731
/// # struct NotAFoo;
@@ -737,25 +738,40 @@ declare_clippy_lint! {
737738
///
738739
/// ```rust
739740
/// # struct Foo;
740-
/// # struct FooError;
741+
/// struct Bar(Foo);
741742
/// impl Foo {
742-
/// // Good. Return type contains `Self`
743-
/// fn new() -> Result<Foo, FooError> {
744-
/// # Ok(Foo)
743+
/// // Bad. The type name must contain `Self`
744+
/// fn new() -> Bar {
745+
/// # Bar(Foo)
745746
/// }
746747
/// }
747748
/// ```
748749
///
749750
/// ```rust
750751
/// # struct Foo;
751-
/// struct Bar(Foo);
752+
/// # struct FooError;
752753
/// impl Foo {
753-
/// // Bad. The type name must contain `Self`.
754-
/// fn new() -> Bar {
755-
/// # Bar(Foo)
754+
/// // Good. Return type contains `Self`
755+
/// fn new() -> Result<Foo, FooError> {
756+
/// # Ok(Foo)
756757
/// }
757758
/// }
758759
/// ```
760+
///
761+
/// Or in a trait definition:
762+
/// ```rust
763+
/// pub trait Trait {
764+
/// // Bad. The type name must contain `Self`
765+
/// fn new();
766+
/// }
767+
/// ```
768+
///
769+
/// ```rust
770+
/// pub trait Trait {
771+
/// // Good. Return type contains `Self`
772+
/// fn new() -> Self;
773+
/// }
774+
/// ```
759775
pub NEW_RET_NO_SELF,
760776
style,
761777
"not returning type containing `Self` in a `new` method"
@@ -1679,8 +1695,6 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16791695

16801696
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
16811697
if_chain! {
1682-
if !in_external_macro(cx.tcx.sess, item.span);
1683-
if !item.span.from_expansion();
16841698
if item.ident.name == sym!(new);
16851699
if let TraitItemKind::Fn(FnSig { decl, .. }, _) = &item.kind;
16861700
if let FnRetTy::Return(ret_ty) = &decl.output;

tests/ui/new_ret_no_self.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ impl MutPointerReturnerOk {
137137
}
138138
}
139139

140-
struct MutPointerReturnerOk2;
140+
struct ConstPointerReturnerOk2;
141141

142-
impl MutPointerReturnerOk2 {
142+
impl ConstPointerReturnerOk2 {
143143
// should not trigger lint
144144
pub fn new() -> *const Self {
145145
unimplemented!();
@@ -283,7 +283,7 @@ mod issue5435 {
283283
}
284284
}
285285

286-
trait MutPointerReturnerOk2 {
286+
trait ConstPointerReturnerOk2 {
287287
// should not trigger lint
288288
fn new() -> *const Self
289289
where

0 commit comments

Comments
 (0)