Skip to content

Commit

Permalink
Fix some quality flaws
Browse files Browse the repository at this point in the history
  • Loading branch information
inverno authored and vilchik-elena committed Apr 21, 2017
1 parent e21375a commit be2d197
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ boolean isProblem(BinaryExpressionTree tree, ProgramState currentState) {
Type rightType = rightConstraint.type();
Type leftType = leftConstraint.type();

if (leftType != null && leftType == rightType) {
return true;
}

return false;
return leftType != null && leftType == rightType;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.sonar.javascript.checks;

import com.google.common.collect.ImmutableSet;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.check.Rule;
Expand Down Expand Up @@ -84,30 +83,26 @@ private void checkForLoop(ForObjectStatementTree forLoop) {

} else if (variableOrExpression.is(Kind.IDENTIFIER_REFERENCE)) {
IdentifierTree identifier = (IdentifierTree) variableOrExpression;
checkSymbol(identifier.symbol(), identifier, forLoop, FOREACH_VARIABLE);

identifier.symbol().ifPresent(s -> checkSymbol(s, identifier, forLoop, FOREACH_VARIABLE));
}
}

private void checkBindingElement(BindingElementTree parameter, String title) {
for (IdentifierTree identifierTree : parameter.bindingIdentifiers()) {
// symbol for identifier from binding element should never be null
checkSymbol(identifierTree.symbol(), identifierTree, null, title);
identifierTree.symbol().ifPresent(s -> checkSymbol(s, identifierTree, null, title));
}
}

private void checkSymbol(Optional<Symbol> symbol, IdentifierTree declarationIdentifier, @Nullable ForObjectStatementTree loop, String title) {
if (!symbol.isPresent()) {
return;
}
private void checkSymbol(Symbol symbol, IdentifierTree declarationIdentifier, @Nullable ForObjectStatementTree loop, String title) {

for (Usage usage : symbol.get().usages()) {
for (Usage usage : symbol.usages()) {

if (usage.isWrite() &&
!usage.identifierTree().equals(declarationIdentifier)
&& (loop == null || CheckUtils.isDescendant(usage.identifierTree(), loop))) {

addIssue(usage.identifierTree(), String.format(MESSAGE, title, symbol.get().name()));
addIssue(usage.identifierTree(), String.format(MESSAGE, title, symbol.name()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void visitNode(Tree tree) {
}
}

private boolean isInfiniteFor(IterationStatementTree loopTree) {
private static boolean isInfiniteFor(IterationStatementTree loopTree) {
if (loopTree.is(Kind.FOR_STATEMENT)) {
ForStatementTree forLoop = (ForStatementTree) loopTree;
return forLoop.update() == null && forLoop.init() == null && forLoop.condition() == null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.sonar.javascript.metrics;

import java.util.Arrays;
import java.util.HashSet;
import java.util.EnumSet;
import java.util.Set;
import org.sonar.javascript.tree.KindSet;
import org.sonar.plugins.javascript.api.tree.Tree;
Expand Down Expand Up @@ -59,7 +59,7 @@ public CounterVisitor(Tree tree) {

@Override
public Set<Kind> nodesToVisit() {
HashSet<Kind> result = new HashSet<>(KindSet.FUNCTION_KINDS.getSubKinds());
Set<Kind> result = EnumSet.copyOf(KindSet.FUNCTION_KINDS.getSubKinds());
result.addAll(Arrays.asList(STATEMENT_NODES));
result.addAll(Arrays.asList(MetricsVisitor.getClassNodes()));
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import java.io.Serializable;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.sonar.api.batch.fs.InputFile;
Expand Down Expand Up @@ -78,7 +78,7 @@ public Map<InputFile, Set<Integer>> executableLines() {

@Override
public Set<Kind> nodesToVisit() {
Set<Kind> result = new HashSet<>(KindSet.FUNCTION_KINDS.getSubKinds());
Set<Kind> result = EnumSet.copyOf(KindSet.FUNCTION_KINDS.getSubKinds());
result.addAll(Arrays.asList(CLASS_NODES));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,10 @@ public SeparatedList<SpecifierTree> exportListBody(
}

public SpecifierListTree exportList(InternalSyntaxToken openCurlyBraceToken, Optional<SeparatedList<SpecifierTree>> specifierList, InternalSyntaxToken closeCurlyBraceToken) {
return new SpecifierListTreeImpl(Kind.EXPORT_LIST, openCurlyBraceToken, specifierList.or(new SeparatedListImpl<>(ImmutableList.of(), ImmutableList.of())), closeCurlyBraceToken);
return new SpecifierListTreeImpl(
Kind.EXPORT_LIST,
openCurlyBraceToken,
specifierList.or(new SeparatedListImpl<>(ImmutableList.of(), ImmutableList.of())), closeCurlyBraceToken);
}

public NameSpaceExportDeclarationTree namespaceExportDeclaration(
Expand Down Expand Up @@ -1216,7 +1219,10 @@ public SeparatedList<SpecifierTree> newImportSpecifierList(
}

public SpecifierListTree importList(InternalSyntaxToken openCurlyBraceToken, Optional<SeparatedList<SpecifierTree>> specifierList, InternalSyntaxToken closeCurlyBraceToken) {
return new SpecifierListTreeImpl(Kind.IMPORT_LIST, openCurlyBraceToken, specifierList.or(new SeparatedListImpl<>(ImmutableList.of(), ImmutableList.of())), closeCurlyBraceToken);
return new SpecifierListTreeImpl(
Kind.IMPORT_LIST,
openCurlyBraceToken,
specifierList.or(new SeparatedListImpl<>(ImmutableList.of(), ImmutableList.of())), closeCurlyBraceToken);
}

public SpecifierTree nameSpaceImport(InternalSyntaxToken starToken, InternalSyntaxToken asToken, IdentifierTree localName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -171,21 +170,19 @@ public boolean hasUsagesInNestedFunctions(Symbol symbol) {

@CheckForNull
private Usage add(IdentifierTree identifier) {
addSymbol(identifier.symbol());
identifier.symbol().ifPresent(s -> addSymbol(s));
Usage usage = localVariableUsages.get(identifier);
if (usage != null) {
usagesInCFG.put(usage.symbol(), usage);
}
return usage;
}

private void addSymbol(Optional<Symbol> symbolOptional) {
if (!symbolOptional.isPresent() || symbols.contains(symbolOptional.get())) {
private void addSymbol(Symbol symbol) {
if (symbols.contains(symbol)) {
return;
}

Symbol symbol = symbolOptional.get();

symbols.add(symbol);

if (isLocalVariable(symbol)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
@javax.annotation.ParametersAreNonnullByDefault
package org.sonar.javascript.se.limitations;
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public List<IdentifierTree> bindingIdentifiers() {
List<IdentifierTree> bindingIdentifiers = Lists.newArrayList();

for (Optional<BindingElementTree> element : elements) {
if (element.isPresent()) {
bindingIdentifiers.addAll(element.get().bindingIdentifiers());
}
element.ifPresent(bindingElementTree -> bindingIdentifiers.addAll(bindingElementTree.bindingIdentifiers()));
}

return bindingIdentifiers;
Expand All @@ -110,10 +108,7 @@ private static class ElidedElementFilter implements Function<Optional<BindingEle

@Override
public BindingElementTree apply(Optional<BindingElementTree> e) {
if (e.isPresent()) {
return e.get();
}
return null;
return e.orElse(null);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ private static class ElidedElementFilter implements Function<Optional<Tree>, Tre

@Override
public Tree apply(Optional<Tree> e) {
if (e.isPresent()) {
return e.get();
}
return null;
return e.orElse(null);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.common.collect.Iterators;
import java.util.Iterator;
import org.sonar.javascript.tree.impl.JavaScriptTree;
import org.sonar.javascript.tree.impl.lexical.InternalSyntaxToken;
import org.sonar.javascript.tree.symbols.type.TypableTree;
import org.sonar.plugins.javascript.api.symbols.Type;
import org.sonar.plugins.javascript.api.symbols.TypeSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.annotation.Nullable;
import org.sonar.javascript.tree.impl.JavaScriptTree;
import org.sonar.plugins.javascript.api.tree.Tree;
import org.sonar.plugins.javascript.api.tree.expression.IdentifierTree;
import org.sonar.plugins.javascript.api.tree.lexical.SyntaxToken;
import org.sonar.plugins.javascript.api.tree.statement.BreakStatementTree;
import org.sonar.plugins.javascript.api.visitors.DoubleDispatchVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.annotation.Nullable;
import org.sonar.javascript.tree.impl.JavaScriptTree;
import org.sonar.plugins.javascript.api.tree.Tree;
import org.sonar.plugins.javascript.api.tree.expression.IdentifierTree;
import org.sonar.plugins.javascript.api.tree.lexical.SyntaxToken;
import org.sonar.plugins.javascript.api.tree.statement.ContinueStatementTree;
import org.sonar.plugins.javascript.api.visitors.DoubleDispatchVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.sonar.javascript.tree.impl.JavaScriptTree;
import org.sonar.javascript.tree.impl.lexical.InternalSyntaxToken;
import org.sonar.plugins.javascript.api.tree.Tree;
import org.sonar.plugins.javascript.api.tree.expression.IdentifierTree;
import org.sonar.plugins.javascript.api.tree.lexical.SyntaxToken;
import org.sonar.plugins.javascript.api.tree.statement.LabelledStatementTree;
import org.sonar.plugins.javascript.api.tree.statement.StatementTree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ private static void inferParameterType(CallExpressionTree tree) {
int minSize = arguments.size() < parameters.size() ? arguments.size() : parameters.size();

for (int i = 0; i < minSize; i++) {
Preconditions.checkState(arguments.get(i) instanceof ExpressionTree);
Tree currentParameter = parameters.get(i);
inferParameterType(currentParameter, arguments, i);
}
Expand Down

0 comments on commit be2d197

Please sign in to comment.