Skip to content

Unused wild card imports are not removed #240

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

  1. added a commit that references this issue on Apr 28, 2018
  2. scottresnik commented on Apr 28, 2018

    @scottresnik
    Author
  3. 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.

  4. changed the title [-]Wild card imports are ignored[/-] [+]Unused wild card imports are not removed[/+] on Apr 28, 2018
  5. 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?

  6. 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)
  7. 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.

  8. 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.

  9. nedtwigg commented on Jul 7, 2025

    @nedtwigg
    Member

    Released in plugin-gradle 7.1.0 and plugin-maven 2.45.0.

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

      Issue actions