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

new_ret_no_self complains about new() -> impl Future<Self> if new() takes parameters #3220

Closed
Arnavion opened this issue Sep 25, 2018 · 5 comments
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@Arnavion
Copy link
Contributor

0.0.212 (2018-09-17 125907a)

Playground

#![feature(tool_lints)]

#![deny(clippy::new_ret_no_self)]

extern crate futures;
use futures::{Future, IntoFuture};

struct S;

impl S {
    pub fn new(_: String) -> impl Future<Item = Self, Error = ()>
    {
        Ok(S).into_future()
    }
}

fn main() {}
error: methods called `new` usually return `Self`
  --> src/main.rs:11:5
   |
11 | /     pub fn new(_: String) -> impl Future<Item = Self, Error = ()>
12 | |     {
13 | |         Ok(S).into_future()
14 | |     }
   | |_____^
   |
note: lint level defined here
  --> src/main.rs:3:9
   |
3  | #![deny(clippy::new_ret_no_self)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#new_ret_no_self

If S::new() does not have the String parameter, the lint does not complain.

@flip1995 flip1995 added the C-bug Category: Clippy is not doing the correct thing label Sep 25, 2018
@JoshMcguigan
Copy link
Contributor

I've attempted to start solving this one and I ran into a couple issues. First, I could not find existing tests for new_ret_no_self, so I created a new test file. I was able to recreate this issue without using an external dependency at the playground below.

https://play.rust-lang.org/?gist=449e1c7a3455843bb7f032fa44a5c7a3&version=stable&mode=debug&edition=2015

I created a test based on this, and then modified this lint slightly, and the compiler crashes when running TESTNAME=ui/new_ret_no_self cargo test --test compile-test with the full test (I commented out the part that breaks it).

aea834f

The actual error message:

error: internal compiler error: librustc_typeck/collect.rs:1487: unexpected sort of node in fn_sig(): ImplItem(ImplItem { id: NodeId(15), ident: Item#0, hir_id: HirId { owner: DefIndex(0:7), local_id: ItemLocalId(0) }, vis: Spanned { node: Inherited, span: tests/ui/new_ret_no_self.rs:13:5: 13:5 }, defaultness: Final, attrs: [], generics: Generics { params: [], where_clause: WhereClause { id: NodeId(16), predicates: [] }, span: tests/ui/new_ret_no_self.rs:1:1: 1:1 }, node: Type(type(Self)), span: tests/ui/new_ret_no_self.rs:13:5: 13:22 })

thread 'main' panicked at 'Box<Any>', librustc_errors/lib.rs:599:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: aborting due to previous error


note: the compiler unexpectedly panicked. this is a bug.

Is this something I am doing, or is it possibly an actual compiler issue?

@Arnavion
Copy link
Contributor Author

Arnavion commented Oct 3, 2018

"The compiler unexpectedly panicked" happens if the compiler API panics. clippy lints plug into the compiler API. So yes, I would start by assuming it's from your change.

@mati865
Copy link
Contributor

mati865 commented Oct 3, 2018

@JoshMcguigan run the test with RUST_BACKTRACE=1 and check the trace. If you need more help you can create WIP PR.

@JoshMcguigan
Copy link
Contributor

Thanks for the help. The relevant part of the trace:

  22: clippy_lints::utils::return_ty
             at clippy_lints/src/utils/mod.rs:747

It was caused by calling cx.tcx.fn_sig(fn_def_id).output() where fn_def_id was the id of something other than a function.

At this point I've fixed the false negative when new doesn't take any arguments, but I'm still working on detecting if the return type is a trait with an associated type of Self. I'll keep going and report back if I have any issues.

@JoshMcguigan
Copy link
Contributor

I opened a PR (#3253) which resolves this issue as well as another issue with this lint, but I'm not sure I did it in the best possible way. I'd be very open to any feedback.

darobs added a commit to Azure/iotedge that referenced this issue Feb 4, 2019
Update clippy to use stable toolchain.  Fix all errors that came about.

Change cfg_attr("cargo-clippy"= allow/deny) to just allow/deny clippy::*

Remove clippy workarounds for new_ret_no_self (clippy bug:
rust-lang/rust-clippy#3220)

Updated devguide
Clippy isn't preview any more, but added instructions to
update toolchain  and rustup if  you already have older version of
Rust installed and can't find rustfmt or clippy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

No branches or pull requests

4 participants