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

Rule S5845: Assertions of dissimilar types should not be made

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.5
    • Component/s: Rules

      Description

      AssertJ cases to support:

      interface KitchenTool {}
      interface Plant {}
      
      class Spatula implements KitchenTool {}
      class Tree implements Plant {}
      
      int size = getSize();
      
      Spatula spatula = getSpatula();
      KitchenTool tool = spatula;
      KitchenTool [] tools = {tool};
      
      Tree tree = getTree();
      Plant plant = tree;
      Tree [] trees = {tree};
      
      // Whatever the given values, those 'isNotEqualTo' assertions are more likely to always pass due to dissimilar types:
      assertThat(size).isNotEqualTo(null);     // Noncompliant; primitives can not be null
      assertThat(spatula).isNotEqualTo(tree);  // Noncompliant; unrelated classes
      assertThat(spatula).isNotEqualTo(plant); // Noncompliant; unrelated class and interface
      assertThat(tool).isNotEqualTo(plant);    // Noncompliant; unrelated interfaces
      assertThat(tool).isNotEqualTo(tools);    // Noncompliant; array & non-array
      assertThat(trees).isNotEqualTo(tools);   // Noncompliant; incompatible arrays
      
      // Other assertions that always pass due to dissimilar types:
      assertThat(size).isNotNull();                        // Noncompliant
      assertThat(spatula).isNotSameAs(tree);               // Noncompliant
      assertThat(spatula).doesNotHaveSameClassAs(tree);    // Noncompliant
      assertThat(spatula).isNotInstanceOf(Tree.class);     // Noncompliant
      assertThat(spatula).isNotExactlyInstanceOf(Tree.class); // Noncompliant
      assertThat(spatula).isNotInstanceOfAny(Tree.class);  // Noncompliant
      assertThat(spatula).isNotOfAnyClassIn(Tree.class);   // Noncompliant
      assertThat(spatula).isNotIn(trees);                  // Noncompliant
      
      // Assertions that always fail due to dissimilar types:
      assertThat(size).isNull();                           // Noncompliant
      assertThat(spatula).isEqualTo(tree);                 // Noncompliant
      assertThat(spatula).isSameAs(tree);                  // Noncompliant
      assertThat(spatula).hasSameClassAs(tree);            // Noncompliant
      assertThat(spatula).isInstanceOf(Tree.class);        // Noncompliant
      assertThat(spatula).isExactlyInstanceOf(Tree.class); // Noncompliant
      assertThat(spatula).isInstanceOfAny(Tree.class);     // Noncompliant
      assertThat(spatula).isOfAnyClassIn(Tree.class);      // Noncompliant
      assertThat(spatula).isIn(trees);                     // Noncompliant
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                alban.auzeill Alban Auzeill
                Reporter:
                alban.auzeill Alban Auzeill
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: