sdedic commented on code in PR #4278:
URL: https://github.com/apache/netbeans/pull/4278#discussion_r906356968


##########
java/java.hints/src/org/netbeans/modules/java/hints/suggestions/Tiny.java:
##########
@@ -400,4 +402,18 @@ protected void performRewrite(final TransformationContext 
ctx) {
 
     }
 
+    @Hint(displayName = 
"#DN_org.netbeans.modules.java.hints.suggestions.Tiny.inlineRedundantVar", 
description = 
"#DESC_org.netbeans.modules.java.hints.suggestions.Tiny.inlineRedundantVar", 
category="suggestions", severity=Severity.HINT)
+    @TriggerPattern(value="$before$; $type $var = $init; $statements$; return 
$var;")
+    public static ErrorDescription inlineRedundantVar(HintContext ctx) {
+        TreePath var = ctx.getVariables().get("$var");
+        Collection<? extends TreePath> stats = 
ctx.getMultiVariables().get("$statements$");
+        if (var != null && stats != null) {
+            Element element = ctx.getInfo().getTrees().getElement(var);
+            if (element != null && element.getKind() == 
ElementKind.LOCAL_VARIABLE && !Utilities.isReferencedIn(ctx.getInfo(), var, 
stats)) {

Review Comment:
   @mbien  I believe it can't be computed in general. If `$statements$` have 
some non-local side effect, then even a method call from `$init` may be 
affected through some field of another object.
   
   There are at least two solutions:
   - simple one: do not call it `redundant` variable, but simply suggest to 
inline the expression :)) It's the user responsibility to decide, and IDE to do 
the refactoring
   - make the constraints tight:
   -- does not reference any potentially non-final symbols nonlocal (that 
includes any invoked methods), or `statements` does not make any outbound 
method/constructor calls. We do not have machinery for nonlocal flow analysis.
   -- `statements` does not *assign* to *any* of the symbols used in `init`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to