openrewrite / rewrite-static-analysis

OpenRewrite recipes for identifying and fixing static analysis issues.
Apache License 2.0
27 stars 43 forks source link

Bug in `InstanceOfPatternMatch` getting the variable name from the name of a nested class #290

Closed nguyenhoan closed 1 month ago

nguyenhoan commented 1 month ago

What version of OpenRewrite are you using?

I am using the latest version at the time of this writing at commit https://github.com/openrewrite/rewrite-static-analysis/commit/1ed9c44b1413a388a7d436e8a7ca5bcacc40fc94

How are you running OpenRewrite?

I am running the unit tests.

What is the smallest, simplest way to reproduce the problem?

Adding this unit test to org.openrewrite.staticanalysis.InstanceOfPatternMatchTest.If

        @Test
        void conflictingVariableOfNestedType() {
            rewriteRun(
              //language=java
              java(
                """
                  import java.util.Map;

                  public class A {
                      void test(Object o) {
                          Map.Entry entry = null;
                          if (o instanceof Map.Entry) {
                            entry = (Map.Entry) o;
                          }
                          System.out.println(entry);
                      }
                  }
                  """,
                """
                  import java.util.Map;

                  public class A {
                      void test(Object o) {
                          Map.Entry entry = null;
                          if (o instanceof Map.Entry entry1) {
                            entry = entry1;
                          }
                          System.out.println(entry);
                      }
                  }
                  """
              )
            );
        }

What did you expect to see?

                  import java.util.Map;

                  public class A {
                      void test(Object o) {
                          Map.Entry entry = null;
                          if (o instanceof Map.Entry entry1) {
                            entry = entry1;
                          }
                          System.out.println(entry);
                      }
                  }

What did you see instead?

                  import java.util.Map;

                  public class A {
                      void test(Object o) {
                          Map.Entry entry = null;
                          if (o instanceof Map.Entry .Entry) {
                            entry = .Entry;
                          }
                          System.out.println(entry);
                      }
                  }

What is the full stack trace of any errors you encountered?

Are you interested in contributing a fix to OpenRewrite?

There's a bug on this line where

                    className = className.substring(className.lastIndexOf('.'));

is missing the increment to go the next character after .

                    className = className.substring(className.lastIndexOf('.') + 1);
timtebeek commented 1 month ago

Hi @nguyenhoan ; thanks a lot for the detailed report, and immediately working out the cause. Would you want to create a PR such that you get credit for the fix as well as a contributor? Just let me know, otherwise I'll make the change myself.

nguyenhoan commented 1 month ago

Thanks for the suggestion!

Here it is https://github.com/openrewrite/rewrite-static-analysis/pull/291