Skip to content

Commit

Permalink
Issue checkstyle#13007: Update doc for UnusedImports
Browse files Browse the repository at this point in the history
  • Loading branch information
TanayMorakhia committed Apr 30, 2023
1 parent 2187858 commit 054429a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@

/**
* <p>
* Checks for unused import statements. Checkstyle uses a simple but very
* reliable algorithm to report on unused import statements. An import statement
* Checks for unused import statements. An import statement
* is considered unused if:
* </p>
* <ul>
Expand All @@ -52,10 +51,6 @@
* checks for imports that handle wild-card imports.
* </li>
* <li>
* It is a duplicate of another import. This is when a class is imported more
* than once.
* </li>
* <li>
* The class imported is from the {@code java.lang} package. For example
* importing {@code java.lang.String}.
* </li>
Expand All @@ -79,23 +74,9 @@
* An imported type has the same name as a declaration, such as a member variable.
* </li>
* <li>
* There are two or more imports with the same name.
* There are two or more static imports with the same method name.
* </li>
* </ul>
* <p>
* For example, in the following case all imports will not be flagged as unused:
* </p>
* <pre>
* import java.awt.Component;
* import static AstTreeStringPrinter.printFileAst;
* import static DetailNodeTreeStringPrinter.printFileAst;
* class FooBar {
* private Object Component; // a bad practice in my opinion
* void method() {
* printFileAst(file); // two imports with the same name
* }
* }
* </pre>
* <ul>
* <li>
* Property {@code processJavadoc} - Control whether to process Javadoc comments.
Expand All @@ -109,6 +90,60 @@
* <pre>
* &lt;module name="UnusedImports"/&gt;
* </pre>
* <p>Example:</p>
* <pre>
* import java.awt.Component; // limitation as it match field name in code
*
* // no ability to recognize what import is not used
* import static AstTreeStringPrinter.printFileAst;
* import static DetailNodeTreeStringPrinter.printFileAst;
*
* import java.lang.String; // violation
*
* import java.util.Stack; // OK
* import java.util.Map; // violation
*
* import java.util.List; // OK
*
* &#47;&#42;&#42;
* &#42; &#64;link List
* &#42;&#47;
* class MyClass{
* Stack stack = new Stack();
* private Object Component;
* }
* </pre>
* <p>
* To configure the check so that it ignores the imports referenced in Javadoc comments:
* </p>
* <pre>
* &lt;module name=&quot;UnusedImports&quot;&gt;
* &lt;property name=&quot;processJavadoc&quot; value=&quot;false&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <p>Example:</p>
* <pre>
* import java.awt.Component; // limitation as it match field name in code
*
* // no ability to recognize what import is not used
* import static AstTreeStringPrinter.printFileAst;
* import static DetailNodeTreeStringPrinter.printFileAst;
*
* import java.lang.String; // violation
*
* import java.util.Stack; // OK
* import java.util.Map; // violation
*
* import java.util.List; // violation
*
* &#47;&#42;&#42;
* &#42; &#64;link List
* &#42;&#47;
* class MyClass{
* Stack stack = new Stack();
* private Object Component;
* }
* </pre>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
name="UnusedImports"
parent="com.puppycrawl.tools.checkstyle.TreeWalker">
<description>&lt;p&gt;
Checks for unused import statements. Checkstyle uses a simple but very
reliable algorithm to report on unused import statements. An import statement
Checks for unused import statements. An import statement
is considered unused if:
&lt;/p&gt;
&lt;ul&gt;
Expand All @@ -16,10 +15,6 @@
checks for imports that handle wild-card imports.
&lt;/li&gt;
&lt;li&gt;
It is a duplicate of another import. This is when a class is imported more
than once.
&lt;/li&gt;
&lt;li&gt;
The class imported is from the {@code java.lang} package. For example
importing {@code java.lang.String}.
&lt;/li&gt;
Expand All @@ -43,23 +38,9 @@
An imported type has the same name as a declaration, such as a member variable.
&lt;/li&gt;
&lt;li&gt;
There are two or more imports with the same name.
There are two or more static imports with the same method name.
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
For example, in the following case all imports will not be flagged as unused:
&lt;/p&gt;
&lt;pre&gt;
import java.awt.Component;
import static AstTreeStringPrinter.printFileAst;
import static DetailNodeTreeStringPrinter.printFileAst;
class FooBar {
private Object Component; // a bad practice in my opinion
void method() {
printFileAst(file); // two imports with the same name
}
}
&lt;/pre&gt;</description>
&lt;/ul&gt;</description>
<properties>
<property default-value="true" name="processJavadoc" type="boolean">
<description>Control whether to process Javadoc comments.</description>
Expand Down
77 changes: 55 additions & 22 deletions src/xdocs/config_imports.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2454,8 +2454,7 @@ public class MyClass{ };
<p>Since Checkstyle 3.0</p>
<subsection name="Description" id="UnusedImports_Description">
<p>
Checks for unused import statements. Checkstyle uses a simple but
very reliable algorithm to report on unused import statements. An
Checks for unused import statements. An
import statement is considered unused if:
</p>

Expand All @@ -2467,11 +2466,6 @@ public class MyClass{ };
for imports that handle wild-card imports.
</li>

<li>
It is a duplicate of another import. This is when a class is
imported more than once.
</li>

<li>
The class imported is from the <code>java.lang</code>
package. For example importing <code>java.lang.String</code>.
Expand Down Expand Up @@ -2499,23 +2493,9 @@ public class MyClass{ };
An imported type has the same name as a declaration, such as a member variable.
</li>
<li>
There are two or more imports with the same name.
There are two or more static imports with the same method name.
</li>
</ul>
<p>
For example, in the following case all imports will not be flagged as unused:
</p>
<source>
import java.awt.Component;
import static AstTreeStringPrinter.printFileAst;
import static DetailNodeTreeStringPrinter.printFileAst;
class FooBar {
private Object Component; // a bad practice in my opinion
void method() {
printFileAst(file); // two imports with the same name
}
}
</source>
</subsection>

<subsection name="Properties" id="UnusedImports_Properties">
Expand Down Expand Up @@ -2546,6 +2526,59 @@ class FooBar {
<source>
&lt;module name=&quot;UnusedImports&quot;/&gt;
</source>
<p>Example:</p>
<source>
import java.awt.Component; // limitation as it match field name in code

// no ability to recognize what import is not used
import static AstTreeStringPrinter.printFileAst;
import static DetailNodeTreeStringPrinter.printFileAst;

import java.lang.String; // violation

import java.util.Stack; // OK
import java.util.Map; // violation

import java.util.List; // OK

&#47;&#42;&#42;
&#42; &#64;link List
&#42;&#47;
class MyClass{
Stack stack = new Stack();
private Object Component;
}
</source>
<p>
To configure the check so that it ignores the imports referenced in Javadoc comments:
</p>
<source>
&lt;module name=&quot;UnusedImports&quot;&gt;
&lt;property name=&quot;processJavadoc&quot; value=&quot;false&quot;/&gt;
&lt;/module&gt;
</source>
<p>Example:</p>
<source>
import java.awt.Component; // limitation as it match field name in code

// no ability to recognize what import is not used
import static AstTreeStringPrinter.printFileAst;
import static DetailNodeTreeStringPrinter.printFileAst;
import java.lang.String; // violation

import java.util.Stack; // OK
import java.util.Map; // violation

import java.util.List; // violation

&#47;&#42;&#42;
&#42; &#64;link List
&#42;&#47;
class MyClass{
Stack stack = new Stack();
private Object Component;
}
</source>
</subsection>

<subsection name="Example of Usage" id="UnusedImports_Example_of_Usage">
Expand Down

0 comments on commit 054429a

Please sign in to comment.