Skip to content

Unused wild card imports are not removed #240

Closed
@scottresnik

Description

@scottresnik

Wild card imports seem to be ignored if they are unused. Is it possible to correct this? At the very least would it be possible to add a setting to fail if wild card imports are encountered?

  • Gradle 4.7
  • Spotless 3.10.0
  • Windows 10 Pro

Activity

added a commit that references this issue on Apr 28, 2018

diffplug/spotless#240 : Example for issue.

scottresnik

scottresnik commented on Apr 28, 2018

@scottresnik
Author
nedtwigg

nedtwigg commented on Apr 28, 2018

@nedtwigg
Member

Consider this situation:

import pkgA.*;
import pkgB.*;

public class Clazz {
    WhatPackageDidIComeFrom field;
}

Without slurping in the classpath, and then looking at the classfiles in those jars, we cannot know which package it came from. Basically requires a full compile. With #239 it might be vaguely possible to do, but it will certainly be a lot of work.

changed the title [-]Wild card imports are ignored[/-] [+]Unused wild card imports are not removed[/+] on Apr 28, 2018
scottresnik

scottresnik commented on Apr 28, 2018

@scottresnik
Author

I kind of figured that would be the case.

What do you think about a setting to fail on cases of wild card imports?

nedtwigg

nedtwigg commented on Apr 28, 2018

@nedtwigg
Member

You can use a custom rule, something like:

custom 'noWildcardImports', {
  if (it.contains('*;\n')) {
    throw new Error('No wildcard imports allowed')
  }
}
bumpThisNumberIfACustomStepChanges(1)
scottresnik

scottresnik commented on Apr 30, 2018

@scottresnik
Author

Thanks @nedtwigg . While that would work, I think the a cleaner solution would be to use one of the static code analysis tools out there.

nedtwigg

nedtwigg commented on Apr 30, 2018

@nedtwigg
Member

Sounds good. Spotless is optimized for code formatting of the form String -> String. If you want to disallow wildcards, you can fudge it with Spotless, or use another tool. I don't think it makes sense to integrate static code analysis with Spotless unless it produces the String -> String output required for spotlessApply to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @scottresnik@nedtwigg

        Issue actions

          Unused wild card imports are not removed · Issue #240 · diffplug/spotless