Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2637

"@NonNull" values should not be set to null

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Hide
      * "xxx" is marked "@yyy" but is set to null.
      * Parameter n to this call is marked "@yyy" but null could be passed.
      * "xxx" is marked "@yyy" but is not initialized in this constructor
      * This method's return value is marked "@yyy" but null is returned.
      Show
      * "xxx" is marked "@yyy" but is set to null. * Parameter n to this call is marked "@yyy" but null could be passed. * "xxx" is marked "@yyy" but is not initialized in this constructor * This method's return value is marked "@yyy" but null is returned.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Objective-C
    • Covered Languages:
      C, C++, Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15min
    • Analysis Scope:
      Main Sources
    • Implementation details:
    • CERT:
      EXP01-J.
    • CWE:
      CWE-476
    • FindBugs:
      NP_NONNULL_.*, NP_NULL_PARAM_DEREF.*,NP_STORE_INTO_NONNULL_FIELD,NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE

      Description

      Fields, parameters and return values marked @NotNull, @NonNull, or @Nonnull are assumed to have non-null values and are not typically null-checked before use. Therefore setting one of these values to null, or failing to set such a class field in a constructor, could cause {{NullPointerException}}s at runtime.

      Noncompliant Code Example

      public class MainClass {
        
        @Nonnull
        private String primary;
        private String secondary;
        
        public MainClass(String color) {
          if (color != null) {
            secondary = null;
          }
          primary = color;  // Noncompliant; "primary" is Nonnull but could be set to null here
        }
        
        public MainClass() { // Noncompliant; "primary" Nonnull" but is not initialized
        }
        
        @Nonnull
        public String indirectMix() {
          String mix = null;
          return mix;  // Noncompliant; return value is Nonnull, but null is returned.}}
        }
      

      See

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                ann.campbell.2 Ann Campbell
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: