Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-4180

S6103 should not raise issues on invocations of methods declared outside the file

    XMLWordPrintable

    Details

    • Type: False-Positive
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Rules
    • Labels:
      None

      Description

      When invoking a method defined in a helper, or any other component outside the file analyzed, S6103 raises an FP.

      package org.example;
      
      import java.util.function.Consumer;
      
      import static org.assertj.core.api.Assertions.assertThat;
      
      public class IllegalArgumentExceptionAssertion {
      
        public static Consumer<IllegalArgumentException> hasMessage(String message) {
          return exception -> {
            assertThat(exception.getMessage()).isEqualTo(message);
          };
        }
      }
      
      package org.example;
      
      import java.util.function.Consumer;
      import org.junit.jupiter.api.Test;
      
      import static org.assertj.core.api.Assertions.assertThat;
      import static org.assertj.core.api.Assertions.assertThatThrownBy;
      
      public class S6103 {
        public static Consumer<IllegalArgumentException> hasMessage(String message) {
          return exception -> {
            assertThat(exception.getMessage()).isEqualTo(message);
          };
        }
      
        @Test
        void test6103() throws Exception {
          assertThatThrownBy(() -> {
            throw new IllegalArgumentException("Message");
          }).isInstanceOfSatisfying(IllegalArgumentException.class, hasMessage("Message"));  // Compliant
        }
      
        @Test
        void test6103Outside() throws Exception {
          assertThatThrownBy(() -> {
            throw new IllegalArgumentException("Message");
          }).isInstanceOfSatisfying(IllegalArgumentException.class, IllegalArgumentExceptionAssertion.hasMessage("Message")); // FP
        }
      }
      

      The issue is raised even when `hasMessage`, declared in the helper, is imported statically.

      DoD: S6103 should not raise issues on the code above.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              dorian.burihabwa Dorian Burihabwa
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: