A local variable is returned or thrown right after it is declared
What it finds
Result r = compute(); return r; names a value only to return it on the next line. The name adds nothing that the method's name does not say, and the two statements can be one. The same holds for a variable that is created and then thrown.
Reported when a local variable with an initializer, declared alone and without annotations, is returned or thrown by the very next statement and used nowhere else. The fix returns the initializer; it is unsafe because the declared type of the variable may convert the value differently from the return type.
From the rule's source, return_fresh_local.rs.
class Factory { String make(int n) { String result = "item" + n;tx0 reports: result is only declared to be returned on the next line return result; } RuntimeException fail(String why) { RuntimeException error = new IllegalStateException(why);tx0 reports: error is only declared to be thrown on the next line throw error; } int twice(int n) { int doubled = n * 2; log(doubled); return doubled; } void log(int n) {}} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:return-fresh-local" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:return-fresh-local] reason="..."tx0 explain tx0/java:return-fresh-local shows the same in the terminal. Found a false positive? Report it.