Skip to content

Commit

Permalink
Return objects directly instead of assigning to temporary variable (s…
Browse files Browse the repository at this point in the history
…potbugs#3175)

* return objects directly

* add changelog entry
  • Loading branch information
JuditKnoll authored Oct 30, 2024
1 parent cf73f0b commit 39959e8
Show file tree
Hide file tree
Showing 47 changed files with 56 additions and 128 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
- Cleanup thread issue and regex issue in test-harness ([#3130](https://github.com/spotbugs/spotbugs/issues/3130))
- Remove extra blank lines and remove public from interface objects as inherently already public ([#3131](https://github.com/spotbugs/spotbugs/issues/3131))
- Fix order of modifiers on properties/methods and ensure correct location in file ([#3132](https://github.com/spotbugs/spotbugs/issues/3132))
- Return objects directly instead of creating more garbage collection by defining them ([#3133](https://github.com/spotbugs/spotbugs/issues/3133))
- Return objects directly instead of creating more garbage collection by defining them ([#3133](https://github.com/spotbugs/spotbugs/pull/3133), [#3175](https://github.com/spotbugs/spotbugs/pull/3175))
- Cleanup double initization and fix comments referring to findbugs instead of spotbugs([#3134](https://github.com/spotbugs/spotbugs/issues/3134))

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,7 @@ private FindbugsPropertyPageTestSubclass createProjectPropertiesPage() {
}

private FindbugsPropertyPageTestSubclass createWorkspacePropertiesPage() {
FindbugsPropertyPageTestSubclass page = new FindbugsPropertyPageTestSubclass();
return page;
return new FindbugsPropertyPageTestSubclass();
}

private String getBugsFileProjectRelativePath() {
Expand All @@ -504,8 +503,7 @@ private String getFilterFileProjectRelativePath() {
}

private Shell getParentShell() {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
return shell;
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
}

private UserPreferences getWorkspacePreferences() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ protected TestScenario getTestScenario() {
}

protected IType getTypeC() throws JavaModelException {
IType type = getJavaProject().findType("C");
return type;
return getJavaProject().findType("C");
}

protected IType getTypeE() throws JavaModelException {
IType type = getJavaProject().findType("C.E");
return type;
return getJavaProject().findType("C.E");
}

private void doNullTest(IType parentType, String anonymousClassNumber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ public class AntPropertyValueProvider implements IAntPropertyValueProvider {
@Override
public String getAntPropertyValue(String antPropertyName) {
if ("findbugs.home".equals(antPropertyName)) {
String home = FindbugsPlugin.getFindBugsEnginePluginLocation();
return home;
return FindbugsPlugin.getFindBugsEnginePluginLocation();
}
throw new IllegalArgumentException("No property " + antPropertyName);
}
Expand Down
3 changes: 1 addition & 2 deletions eclipsePlugin/src/de/tobject/findbugs/FindbugsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -1134,8 +1134,7 @@ public static Set<BugPattern> getKnownPatterns() {
}

public static Set<BugCode> getKnownPatternTypes() {
Set<BugCode> patterns = new TreeSet<>(DetectorFactoryCollection.instance().getBugCodes());
return patterns;
return new TreeSet<>(DetectorFactoryCollection.instance().getBugCodes());
}

public static Set<String> getFilteredIds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ protected final IProject getProject(IStructuredSelection structuredSelection) {
if (resource == null) {
return null;
}
IProject project = resource.getProject();
return project;
return resource.getProject();
}

protected String getDialogSettingsId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ protected void runWithProgress(IProgressMonitor monitor) throws CoreException {
try {
bugCollection.writeXML(fileName);
} catch (IOException e) {
CoreException ex = new CoreException(FindbugsPlugin.createErrorStatus(
throw new CoreException(FindbugsPlugin.createErrorStatus(
"Can't write SpotBugs bug collection from project " + project + " to file " + fileName, e));
throw ex;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,7 @@ private SortedBugCollection mergeBugCollections(SortedBugCollection firstCollect
// unknown bug instances appearing (merged collection doesn't contain
// all bugs)
boolean copyDeadBugs = incremental;
SortedBugCollection merged = (SortedBugCollection) (update.mergeCollections(firstCollection, secondCollection,
copyDeadBugs, incremental));
return merged;
return (SortedBugCollection) (update.mergeCollections(firstCollection, secondCollection, copyDeadBugs, incremental));
}

private Map<String, Boolean> relativeToAbsolute(Map<String, Boolean> map) {
Expand Down Expand Up @@ -486,8 +484,7 @@ private static IPath getRelativePath(IPath filePath, IPath commonPath) {
return filePath;
}
// since Equinox 3.5 we can use IPath.makeRelativeTo(IPath)
IPath relativeTo = filePath.makeRelativeTo(commonPath);
return relativeTo;
return filePath.makeRelativeTo(commonPath);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions eclipsePlugin/src/de/tobject/findbugs/builder/WorkItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ public Set<IMarker> getMarkers(boolean recursive) {
recursive = true;
}
IMarker[] markers = MarkerUtil.getMarkers(markerTarget, recursive ? IResource.DEPTH_INFINITE : IResource.DEPTH_ONE);
Set<IMarker> forJavaElement = MarkerUtil.findMarkerForJavaElement(javaElt, markers, recursive);
return forJavaElement;
return MarkerUtil.findMarkerForJavaElement(javaElt, markers, recursive);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ protected final void configureAction(Action action, String textKey, String toolt
* @return IWorkbenchSiteProgressService or <code>null</code>.
*/
protected IWorkbenchSiteProgressService getProgressService() {
IWorkbenchSiteProgressService service = (IWorkbenchSiteProgressService) getSite().getAdapter(
IWorkbenchSiteProgressService.class);
return service;
return getSite().getAdapter(IWorkbenchSiteProgressService.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public String getText(Object element) {
}
int filtered = getFilteredMarkersCount(group);
String filterCount = filtered > 0 ? "/" + filtered + " filtered" : "";
String str = group.getShortDescription() + " (" + (group.getMarkersCount() - filtered) + filterCount + ")";
return str;
return group.getShortDescription() + " (" + (group.getMarkersCount() - filtered) + filterCount + ")";
}
if (element instanceof IMarker) {
IMarker marker = (IMarker) element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ protected void clearText() {

public boolean isFiltering() {
String filterString = getFilterString();
boolean yes = filterString != null && filterString.length() > 0 && !filterString.equals(getInitialText());
return yes;
return filterString != null && filterString.length() > 0 && !filterString.equals(getInitialText());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ private String[] createCommandArgumentsArray(List<File> fileList) {
parts.add(f.getAbsolutePath());
}

String[] args = parts.toArray(new String[parts.size()]);
return args;
return parts.toArray(new String[parts.size()]);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/BugInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -2080,8 +2080,7 @@ private int ageInDays(BugCollection bugCollection, long firstSeen) {
if (age < 0) {
age = 0;
}
int ageInDays = (int) (age / 1000 / 3600 / 24);
return ageInDays;
return (int) (age / 1000 / 3600 / 24);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,7 @@ private String findFullyQualifiedSourceFileName(IClassPath classPath, ClassDescr
packageName += "/";
}

String fullyQualifiedSourceFile = packageName + sourceFile;

return fullyQualifiedSourceFile;
return packageName + sourceFile;
} catch (CheckedAnalysisException e) {
errorLogger.logError("Could scan class " + classDesc.toDottedClassName(), e);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,7 @@ public int compareMethods(BugCollection lhsCollection, BugCollection rhsCollecti
}

// Compare for exact match
int cmp = lhsMethod.compareTo(rhsMethod);

return cmp;
return lhsMethod.compareTo(rhsMethod);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/PackageStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ private ClassStats getClassStats(String name, String sourceFile) {
}

public @CheckForNull ClassStats getClassStatsOrNull(String name) {
ClassStats result = packageMembers.get(name);
return result;
return packageMembers.get(name);
}

@Override
Expand Down
6 changes: 2 additions & 4 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/PluginLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1268,8 +1268,7 @@ private static Date parseDate(String releaseDate) {
}
try {
SimpleDateFormat releaseDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm aa z", Locale.ENGLISH);
Date result = releaseDateFormat.parse(releaseDate);
return result;
return releaseDateFormat.parse(releaseDate);
} catch (ParseException e) {
AnalysisContext.logError("unable to parse date " + releaseDate, e);
return null;
Expand Down Expand Up @@ -1630,8 +1629,7 @@ private static Document parseDocument(@WillClose InputStream in) throws Document
Reader r = UTF8.bufferedReader(in);
try {
SAXReader reader = XMLUtil.buildSAXReader();
Document d = reader.read(r);
return d;
return reader.read(r);
} finally {
Util.closeSilently(r);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,8 @@ public static SourceLineAnnotation createReallyUnknown(@DottedClassName String c
*/
@Nonnull
public static SourceLineAnnotation createUnknown(@DottedClassName String className, String sourceFile, int startBytecode, int endBytecode) {
SourceLineAnnotation result = new SourceLineAnnotation(className, sourceFile, -1, -1, startBytecode, endBytecode);
return new SourceLineAnnotation(className, sourceFile, -1, -1, startBytecode, endBytecode);
// result.setDescription("SOURCE_LINE_UNKNOWN");
return result;
}

/**
Expand All @@ -227,10 +226,7 @@ public static SourceLineAnnotation createUnknown(@DottedClassName String classNa
* @return the SourceLineAnnotation
*/
public static SourceLineAnnotation fromVisitedMethod(PreorderVisitor visitor) {

SourceLineAnnotation sourceLines = getSourceAnnotationForMethod(visitor.getDottedClassName(), visitor.getMethodName(),
visitor.getMethodSig());
return sourceLines;
return getSourceAnnotationForMethod(visitor.getDottedClassName(), visitor.getMethodName(), visitor.getMethodSig());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ public void accept(BugAnnotationVisitor visitor) {

@Override
public String format(String key, ClassAnnotation primaryClass) {
String txt = value;
return txt;
return value;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ public boolean isAssertionInstruction(Instruction ins, ConstantPoolGen cpg) {
}

public boolean isAssertionCall(InvokeInstruction inv) {

boolean isAssertionMethod = assertionMethodRefSet.get(inv.getIndex());

return isAssertionMethod;
return assertionMethodRefSet.get(inv.getIndex());
}
}
4 changes: 1 addition & 3 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ClassHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,7 @@ public boolean equals(Object o) {
*/
@Override
public int compareTo(ClassHash other) {
int cmp = MethodHash.compareHashes(this.classHash, other.classHash);
// System.out.println(this + " <=> " + other + ": compareTo=" + cmp);
return cmp;
return MethodHash.compareHashes(this.classHash, other.classHash);
}

/*
Expand Down
5 changes: 1 addition & 4 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/ba/Hierarchy.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,7 @@ public static ObjectType[] findDeclaredExceptions(InvokeInstruction inv, Constan
for (Method method : methodList) {
if (method.getName().equals(methodName) && method.getSignature().equals(methodSig)
&& accessFlagsAreConcrete(method.getAccessFlags())) {
JavaClassAndMethod m = new JavaClassAndMethod(javaClass, method);

return m;

return new JavaClassAndMethod(javaClass, method);
}
}
if (DEBUG_METHOD_LOOKUP) {
Expand Down
3 changes: 1 addition & 2 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/ba/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public boolean isLastInstructionInBasicBlock() {

@Override
public int compareTo(Location other) {
int pos = handle.getPosition() - other.handle.getPosition();
return pos;
return handle.getPosition() - other.handle.getPosition();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ public NullnessAnnotation getResolvedAnnotation(final Object o, boolean getMinim
return NullnessAnnotation.NONNULL;
}
}
NullnessAnnotation result = super.getResolvedAnnotation(o, getMinimal);
return result;
return super.getResolvedAnnotation(o, getMinimal);
} finally {
profiler.end(this.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ int[] getParameterOffset() {

public int getSlotsFromTopOfStackForParameter(int paramNum) {
int offset = getParameterOffset()[paramNum];
int result = totalArgumentSize - offset;
return result;
return totalArgumentSize - offset;
}

private class ParameterSignatureIterator implements Iterator<String> {
Expand Down
6 changes: 2 additions & 4 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/ba/XFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,7 @@ public static XMethod createXMethod(InvokeInstruction invokeInstruction, Constan
public static XMethod createXMethod(PreorderVisitor visitor) {
JavaClass javaClass = visitor.getThisClass();
Method method = visitor.getMethod();
XMethod m = createXMethod(javaClass, method);
return m;
return createXMethod(javaClass, method);
}

/**
Expand All @@ -662,8 +661,7 @@ public static XMethod createXMethod(PreorderVisitor visitor) {
public static XField createXField(PreorderVisitor visitor) {
JavaClass javaClass = visitor.getThisClass();
Field field = visitor.getField();
XField f = createXField(javaClass, field);
return f;
return createXField(javaClass, field);
}

public static XMethod createXMethod(MethodGen methodGen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ public void clearDerefSet(ValueNumber value) {
* @return the set of dereference Locations
*/
public Set<Location> getDerefLocationSet(ValueNumber vn) {
Set<Location> derefLocationSet = derefLocationSetMap.computeIfAbsent(vn, k -> new HashSet<>());
return derefLocationSet;
return derefLocationSetMap.computeIfAbsent(vn, k -> new HashSet<>());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ public static TypeQualifierAnnotation constructTypeQualifierAnnotation(Annotatio
When when = whenValue == null ? When.ALWAYS : When.valueOf(whenValue.value);
ClassDescriptor annotationClass = v.getAnnotationClass();
TypeQualifierValue<?> tqv = TypeQualifierValue.getValue(annotationClass, v.getValue("value"));
TypeQualifierAnnotation tqa = TypeQualifierAnnotation.getValue(tqv, when);
return tqa;
return TypeQualifierAnnotation.getValue(tqv, when);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,9 @@ private static Set<? extends SourceSinkInfo> getSourceSinkInfoSet(Map<ValueNumbe

private static Set<SourceSinkInfo> getOrCreateSourceSinkInfoSet(Map<ValueNumber, Set<SourceSinkInfo>> sourceSinkInfoSetMap,
ValueNumber vn) {
Set<SourceSinkInfo> sourceSinkInfoSet = sourceSinkInfoSetMap.computeIfAbsent(vn, k -> new HashSet<>(3));
return sourceSinkInfoSet;
return sourceSinkInfoSetMap.computeIfAbsent(vn, k -> new HashSet<>(3));
}



public FlowValue getValue(ValueNumber vn) {
FlowValue result = valueMap.get(vn);
return result != null ? result : FlowValue.TOP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public class ParameterNullnessPropertyDatabase extends MethodPropertyDatabase<Pa
protected ParameterProperty decodeProperty(String propStr) throws PropertyDatabaseFormatException {
try {
int unconditionalDerefSet = Integer.parseInt(propStr);
ParameterProperty prop = new ParameterProperty(unconditionalDerefSet);
return prop;
return new ParameterProperty(unconditionalDerefSet);
} catch (NumberFormatException e) {
throw new PropertyDatabaseFormatException("Invalid unconditional deref param set: " + propStr);
}
Expand Down
Loading

0 comments on commit 39959e8

Please sign in to comment.