All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S1128.html Maven / Gradle / Ivy

There is a newer version: 8.10.0.38194
Show newest version

Why is this an issue?

Unnecessary imports refer to importing types that are not used or referenced anywhere in the code.

Although they don’t affect the runtime behavior of the application after compilation, removing them will:

  • Improve the readability and maintainability of the code.
  • Help avoid potential naming conflicts.
  • Improve the build time, as the compiler has fewer lines to read and fewer types to resolve.
  • Reduce the number of items the code editor will show for auto-completion, thereby showing fewer irrelevant suggestions.

Exceptions

Imports for types mentioned in Javadocs are ignored.

How to fix it

While it’s not difficult to remove these unneeded lines manually, modern code editors support the removal of every unnecessary import with a single click from every file of the project.

Code examples

Noncompliant code example

package myapp.helpers;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.*;     // Noncompliant - package is imported twice
import java.lang.Runnable;  // Noncompliant - java.lang is imported by default

public class FileHelper {
    public static String readFirstLine(String filePath) throws IOException {
        return Files.readAllLines(Paths.get(filePath)).get(0);
    }
}

Compliant solution

package myapp.helpers;

import java.io.IOException;
import java.nio.file.*;

public class FileHelper {
    public static String readFirstLine(String filePath) throws IOException {
        return Files.readAllLines(Paths.get(filePath)).get(0);
    }
}

Resources

Documentation

Related rules

  • {rule:java:S1144} - Unused "private" methods should be removed
  • {rule:java:S1481} - Unused local variables should be removed




© 2015 - 2025 Weber Informatics LLC | Privacy Policy