Best way to handle false unused imports in intellij
Asked Answered
O

3

19

Intellij falsely marked some import of Scala implicits as not being use. Is there a way to prevent it from deleting those import when optimized them explicitly for a specific import and not prevent optimized import for the entire project ?

Overcloud answered 3/4, 2017 at 7:16 Comment(2)
could you give an example? From memory, I think it happens for some implicit resolution imports, but I'm not quite sure of an explicit example to give.Rus
This can be necessary if IntelliJ can't figure out that the imports are used. For example, github.com/xdotai/… generates code with macros, so I'm guessing that IntelliJ will just remove some of the imports since it can't see the code that's generated.Samothrace
H
13

I'm afraid there isn't, I had similar issues especially when using akka and importing the implicit execution context from an ActorSystem in some cases. I recommend defining the value instead of importing. One such example would be:

// Avoid importing the execution context like this
class MyActor extends Actor {
  import context.system.dispatcher
}

// Define it explicitly instead
class MyActor extends Actor {
  implicit val ec = context.system.dispatcher
}

I hope this helps you.

Hayley answered 3/4, 2017 at 9:3 Comment(4)
I was hopping for something more elegant something like just suppressing the warning, this solution can be ugly if you have more than a few implicits to import :) but this will do the work.. thx!Overcloud
Yeah, you are right it can get quite messy, but unfortunately that's the only workaround I know :(Hayley
Is this still the case?Vidicon
This will then become an unused val. But it looks like then you can add @unused above it and it does not show as grayed out. I personally also added a comment in front of it that it is actually used for the humans reading it! :DDiscourteous
P
14

IntelliJ's Scala plugin now allows you to suppress this false warning on a project-wide level. That may not be appropriate for all cases, but it can help. Click the lightbulb then select "Mark import as always used in this project"

enter image description here

Alternatively you can add this directly to your code style xml:

<component name="ProjectCodeStyleConfiguration">
  <code_scheme name="Project" version="173">
    <ScalaCodeStyleSettings>
      <option name="alwaysUsedImports">
        <array>
          <option value="models.slickless.synchronized" />
          <option value="another.import.path" />
        </array>
      </option>
    </ScalaCodeStyleSettings>
  </code_scheme>
</component>

IntelliJ usually stores that file at .idea/codeStyles/Project.xml. It may be a good idea to commit this to your repository so it can be shared.

Parada answered 28/11, 2018 at 23:41 Comment(0)
H
13

I'm afraid there isn't, I had similar issues especially when using akka and importing the implicit execution context from an ActorSystem in some cases. I recommend defining the value instead of importing. One such example would be:

// Avoid importing the execution context like this
class MyActor extends Actor {
  import context.system.dispatcher
}

// Define it explicitly instead
class MyActor extends Actor {
  implicit val ec = context.system.dispatcher
}

I hope this helps you.

Hayley answered 3/4, 2017 at 9:3 Comment(4)
I was hopping for something more elegant something like just suppressing the warning, this solution can be ugly if you have more than a few implicits to import :) but this will do the work.. thx!Overcloud
Yeah, you are right it can get quite messy, but unfortunately that's the only workaround I know :(Hayley
Is this still the case?Vidicon
This will then become an unused val. But it looks like then you can add @unused above it and it does not show as grayed out. I personally also added a comment in front of it that it is actually used for the humans reading it! :DDiscourteous
P
3

Unfortunately there isn't a fix and the problem appears to originate in the compiler itself.

See https://youtrack.jetbrains.com/issue/SCL-7335 which leads to https://issues.scala-lang.org/browse/SI-8773 (reported in 2014 but no fix yet).

Procurance answered 10/8, 2017 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.