Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

"Add use foo:bar" quick fix insert location incorrect #1085

Closed
phrohdoh opened this issue Oct 12, 2018 · 2 comments
Closed

"Add use foo:bar" quick fix insert location incorrect #1085

phrohdoh opened this issue Oct 12, 2018 · 2 comments

Comments

@phrohdoh
Copy link

Given the following code (where the trait Bar is not in scope):

#[derive(Clone, Debug)] // this is line 1 in this file
pub struct Service;

pub struct Response<T> where T: Bar { pub item: T }

Executing the "Add use foo::bar" quick fix will add the appropriate import but in the wrong location.

It will always pick to insert the line just above the Service struct declaration.

This is the case if the attribute line is commented out (understandable) and even if there are multiple empty lines between the struct decl and the attributes.

This isn't desired because I am now suddenly applying attributes to a use which is invalid.


I haven't been able to reduce this in a way that only reports a single fix (like what I described above).

@alexheretic
Copy link
Member

This is actually a rustc issue as that's the source of the suggestion. You can even see it in cargo check output.

error[E0405]: cannot find trait `Bar` in this scope                                                                                       
 --> src/main.rs:4:33                                                                                                                     
  |                                                                                                                                       
4 | pub struct Response<T> where T: Bar { pub item: T }                                                                                   
  |                                 ^^^ not found in this scope                                                                           
help: possible candidate is found in another module, you can import it into scope                                                         
  |                                                                                                                                       
2 | use crate::foo::Bar; 

The 2 | bit means rustc reckons this should be inserted on line 2.

I'd guess rustc has some pretty simple logic for import suggestion locations.

  • On top of the first use line.
  • If no use, at the top of the file, but under # lines.

The latter was probably to place imports under things like #![windows_subsystem = "windows"] where if placed above would cause compile error.

@nrc
Copy link
Member

nrc commented Oct 25, 2018

Moved to Rust repo: rust-lang/rust#55335

@nrc nrc closed this as completed Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants