Checker FieldAccess

belongs to group Basic
Identify fields never read or never written

Frameworks supported by this checker

  • java up to 11
  • android up to API level 28
  • dotnet

Warnings generated by this checker

  • FieldNeverReadWarning: a field is never read in the code [ CWE772 ]
  • FieldNeverUsedWarning: a field is never read nor written in the code [ CWE398 ]
  • FieldNeverWrittenWarning: a field is never written in the code [ CWE456 ]

Options accepted by this checker

  • none

Annotations understood by this checker

  • none


Description

Fields constitute the state of an object, that is, they are meant to store information that survives across method calls. It is hence useless to store information in a field that is never read. Moreover, that behavior is actually harmful, since writing into a useless field hinders the garbage collector. It is also pointless to read data from a field that is never written, since the default value for the field will be found there. Note that there are situations when a field is written by reflection, which cannot be understood by Julia, in general. This is often the case of fields auto-wired through dependency injection. Julia knows some specific patterns, typically marked through code annotations, but cannot foresee all possibilities.

Action: remove all fields that are never read or never written.

Fields constitute the state of an object, that is, they are meant to store information that survives across method calls. It is hence useless to store information in a field that is never read. Moreover, that behavior is actually harmful, since writing into a useless field hinders the garbage collector. It is also pointless to read data from a field that is never written, since the default value for the field will be found there. Note that there are situations when a field is written by reflection, which cannot be understood by Julia, in general. This is often the case of fields auto-wired through dependency injection. Julia knows some specific patterns, typically marked through code annotations, but cannot foresee all possibilities.

Action: remove all fields that are never read or never written.

Examples

Consider the following program:

public class Fields {
  private int f1;
  private final Object f2 = new Object();
  private float f3;

  public Fields() {
    System.out.println(f1);
  }

  public static void main(String args[]) {
    new Fields();
  }
}

This checker issues the following warnings:

Fields.java: [FieldAccess: FieldNeverReadWarning] Field Fields.f2 is never read in reachable code
Fields.java: [FieldAccess: FieldNeverUsedWarning] Field Fields.f3 is never used in reachable code
Fields.java: [FieldAccess: FieldNeverWrittenWarning] Field Fields.f1 is never written in reachable code

In this example, all three fields are useless and could be removed, reducing the memory footprint of the instances of the class and helping the garbage collector.

Consider the following program:

using System;


namespace DocumentationExamples
{

    public class FieldAccess
    {
        private int f1;
        private readonly object f2 = new object();
        private float f3;
        public FieldAccess()
        {
            Console.WriteLine(f1);
        }
        public static void Main(string[] args)
        {
            new FieldAccess();
        }
    }
}

This checker issues the following warnings:

DocumentationExamples.cs: [FieldAccess: FieldNeverReadWarning] Field "f2" is never read in reachable code
DocumentationExamples.cs: [FieldAccess: FieldNeverUsedWarning] Field "f3" is never used in reachable code
DocumentationExamples.cs: [FieldAccess: FieldNeverWrittenWarning] Field "f1" is never written in reachable code

In this example, all three fields are useless and could be removed, reducing the memory footprint of the instances of the class.