Repository navigation
DO NOT MERGE SemSitter front-end: align unused imports, enum constant annotations and generic cast operands with ECJ #6620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: rombirli/semsitter-pvf
Are you sure you want to change the base?
Changes from all commits
8705656
7ccb007
29dee07
1b0346b
a6a8113
a92bd55
7903c2e
f1d4df9
cd694f0
f21dae2
41874c5
1062113
849e49f
3a3c04a
fc1735d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,12 +28,14 @@ | |
| import java.util.Set; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import java.util.stream.Collectors; | ||
| import org.sonar.java.model.ExpressionUtils; | ||
| import org.sonar.java.model.JavaTree; | ||
| import org.sonar.java.model.expression.IdentifierTreeImpl; | ||
| import org.sonar.plugins.java.api.diagnostics.ProblemKind; | ||
| import org.sonar.plugins.java.api.semantic.Symbol; | ||
| import org.sonar.plugins.java.api.semantic.Type; | ||
| import org.sonar.plugins.java.api.tree.AnnotationTree; | ||
| import org.sonar.plugins.java.api.tree.AssignmentExpressionTree; | ||
| import org.sonar.plugins.java.api.tree.BaseTreeVisitor; | ||
| import org.sonar.plugins.java.api.tree.BinaryExpressionTree; | ||
|
|
@@ -44,15 +46,18 @@ | |
| import org.sonar.plugins.java.api.tree.IdentifierTree; | ||
| import org.sonar.plugins.java.api.tree.ImportClauseTree; | ||
| import org.sonar.plugins.java.api.tree.ImportTree; | ||
| import org.sonar.plugins.java.api.tree.LiteralTree; | ||
| import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; | ||
| import org.sonar.plugins.java.api.tree.MethodReferenceTree; | ||
| import org.sonar.plugins.java.api.tree.MethodTree; | ||
| import org.sonar.plugins.java.api.tree.ModuleDeclarationTree; | ||
| import org.sonar.plugins.java.api.tree.NewArrayTree; | ||
| import org.sonar.plugins.java.api.tree.PackageDeclarationTree; | ||
| import org.sonar.plugins.java.api.tree.SyntaxToken; | ||
| import org.sonar.plugins.java.api.tree.SyntaxTrivia; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
| import org.sonar.plugins.java.api.tree.TryStatementTree; | ||
| import org.sonar.plugins.java.api.tree.TypeParameterTree; | ||
| import org.sonar.plugins.java.api.tree.TypeTree; | ||
| import org.sonar.plugins.java.api.tree.UnionTypeTree; | ||
| import org.sonar.plugins.java.api.tree.VariableTree; | ||
|
|
@@ -151,6 +156,12 @@ | |
| scan(tree.method().annotations()); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitTypeParameter(TypeParameterTree tree) { | ||
| scan(tree.identifier().annotations()); | ||
| super.visitTypeParameter(tree); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitTypeCast(org.sonar.plugins.java.api.tree.TypeCastTree tree) { | ||
| Type cast = tree.type().symbolType(); | ||
|
|
@@ -206,14 +217,17 @@ | |
|
|
||
| /** Flow analysis is not done for a unit that has unresolved names. */ | ||
| private void reportUnreachableCatches(JavaTree.CompilationUnitTreeImpl unit) { | ||
| String currentPackage = unit.packageDeclaration() == null ? "" : dotted(unit.packageDeclaration().packageName()); | ||
| boolean unresolved = identifiers.stream().anyMatch(identifier -> identifier.symbol().isUnknown() && !isUnnamed(identifier)) | ||
| || unit.imports().stream().anyMatch(clause -> clause instanceof ImportTree tree && !isResolvable(tree, dotted(tree.qualifiedIdentifier()), currentPackage)); | ||
| if (!unresolved) { | ||
| if (!hasUnresolvedNames(unit)) { | ||
| tries.forEach(this::reportUnreachableCatches); | ||
| } | ||
| } | ||
|
|
||
| private boolean hasUnresolvedNames(JavaTree.CompilationUnitTreeImpl unit) { | ||
| String currentPackage = unit.packageDeclaration() == null ? "" : dotted(unit.packageDeclaration().packageName()); | ||
| return identifiers.stream().anyMatch(identifier -> identifier.symbol().isUnknown() && !isUnnamed(identifier)) | ||
| || unit.imports().stream().anyMatch(clause -> clause instanceof ImportTree tree && !isResolvable(tree, dotted(tree.qualifiedIdentifier()), currentPackage)); | ||
| } | ||
|
|
||
| private static boolean isUnnamed(IdentifierTree identifier) { | ||
| return identifier instanceof IdentifierTreeImpl impl && impl.isUnnamedVariable(); | ||
| } | ||
|
|
@@ -362,12 +376,16 @@ | |
| return text.length(); | ||
| } | ||
|
|
||
| private void reportUnusedImports(JavaTree.CompilationUnitTreeImpl unit) { | ||
|
Check failure on line 379 in java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java
|
||
| if (unit.moduleDeclaration() != null) { | ||
| // The compiler reports no unused import in a module declaration unit. | ||
| return; | ||
| } | ||
| if (suppressesUnused(unit)) { | ||
| return; | ||
| } | ||
| Set<String> seen = new HashSet<>(); | ||
| Set<String> declared = declaredTypeNames(unit); | ||
| String currentPackage = unit.packageDeclaration() == null ? "" : dotted(unit.packageDeclaration().packageName()); | ||
| loadImportedTypes(unit); | ||
| List<IdentifierTree> outOfScope = identifiers.stream().filter(identifier -> !isInFileScope(identifier)).toList(); | ||
|
|
@@ -378,12 +396,38 @@ | |
| String name = dotted(importTree.qualifiedIdentifier()); | ||
| String key = (importTree.isStatic() ? "static " : "") + name; | ||
| boolean duplicate = !seen.add(key); | ||
|
Comment on lines
384
to
398
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Quality: PR description still claims unresolved-name units report no unused importThis commit removes the Was this helpful? React with 👍 / 👎 |
||
| if (isResolvable(importTree, name, currentPackage) && (duplicate || isOwnPackage(importTree, name, currentPackage) || !isUsed(importTree, name, outOfScope))) { | ||
| boolean ownType = !importTree.isStatic() && importTree.qualifiedIdentifier() instanceof MemberSelectExpressionTree select && declared.contains(select.identifier().name()); | ||
| if (!ownType && isResolvable(importTree, name, currentPackage) && (duplicate || isOwnPackage(importTree, name, currentPackage) || !isUsed(importTree, name, outOfScope))) { | ||
| add(ProblemKind.UNUSED_IMPORT, "The import " + (name.endsWith(".*") ? name.substring(0, name.length() - 2) : name) + " is never used", importTree); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** The compiler honours a suppression of unused code on the first type declaration of the unit for its imports too. */ | ||
| private static boolean suppressesUnused(JavaTree.CompilationUnitTreeImpl unit) { | ||
| return unit.types().stream().filter(ClassTree.class::isInstance).map(ClassTree.class::cast).findFirst() | ||
| .map(first -> first.modifiers().annotations().stream().anyMatch(CompilerWarnings::suppressesUnused)).orElse(false); | ||
| } | ||
|
|
||
| /** The compiler never reports a single-type import of a type the unit itself declares. */ | ||
| private static Set<String> declaredTypeNames(JavaTree.CompilationUnitTreeImpl unit) { | ||
| return unit.types().stream().filter(ClassTree.class::isInstance).map(ClassTree.class::cast) | ||
| .map(type -> type.simpleName() == null ? "" : type.simpleName().name()).collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| private static boolean suppressesUnused(AnnotationTree annotation) { | ||
| String name = dotted(annotation.annotationType()); | ||
| if (!"SuppressWarnings".equals(name) && !"java.lang.SuppressWarnings".equals(name)) { | ||
| return false; | ||
| } | ||
| return annotation.arguments().stream().anyMatch(argument -> { | ||
| ExpressionTree value = argument instanceof AssignmentExpressionTree assignment ? assignment.expression() : (ExpressionTree) argument; | ||
| List<? extends Tree> literals = value instanceof NewArrayTree array ? array.initializers() : List.of(value); | ||
| return literals.stream().anyMatch(literal -> literal instanceof LiteralTree string && string.is(Tree.Kind.STRING_LITERAL) | ||
| && Set.of("\"unused\"", "\"all\"").contains(string.value())); | ||
| }); | ||
| } | ||
|
|
||
| /** Loads the imported types first, so that an on-demand import of their package is known to exist. */ | ||
| private void loadImportedTypes(JavaTree.CompilationUnitTreeImpl unit) { | ||
| for (ImportClauseTree clause : unit.imports()) { | ||
|
|
@@ -470,7 +514,7 @@ | |
| if (!importTree.isStatic()) { | ||
| return denotesType(symbol, dotted, identifier); | ||
| } | ||
| return isMemberOf(symbol, qualifier.replace('$', '.')) || (symbol.isTypeSymbol() && dottedType(symbol).equals(dotted)); | ||
| return isMemberOf(symbol, qualifier.replace('$', '.')) || denotesType(symbol, dotted, identifier); | ||
| } | ||
|
|
||
| /** A static import also brings in the members the named type inherits. */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,7 @@ private static final class Caches { | |
| private Function<String, PackagePresence> packageLoader = name -> PackagePresence.UNKNOWN; | ||
| private int typeRevision; | ||
| private boolean noLibraries; | ||
| private int release; | ||
| private boolean typesLoaded; | ||
| } | ||
|
|
||
|
|
@@ -319,6 +320,15 @@ void moveUse(@Nullable String from, @Nullable String to, IdentifierTree use) { | |
| } | ||
|
|
||
| /** Without any library, a method of a type nothing declares is unknown instead of named after its written owner. */ | ||
| void release(int release) { | ||
| caches.release = release; | ||
| } | ||
|
|
||
| /** The configured language level, or 0 when the project sets none. */ | ||
| int release() { | ||
| return caches.release; | ||
| } | ||
|
|
||
| void noLibraries(boolean noLibraries) { | ||
| caches.noLibraries = noLibraries; | ||
| } | ||
|
Comment on lines
322
to
334
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Quality: JSema release(): javadoc moved off noLibraries and wrong defaultThe new Move the noLibraries javadoc back and document the unset value accurately:
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎 |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.