Este producto no es compatible con el sitio Datadog seleccionado. ().
Esta página aún no está disponible en español. Estamos trabajando en su traducción. Si tienes alguna pregunta o comentario sobre nuestro actual proyecto de traducción, no dudes en ponerte en contacto con nosotros.
Metadata
ID:java-best-practices/useless-null
Language: Java
Severity: Notice
Category: Best Practices
Description
This rule flags null checks (== null or != null) performed on expressions that are guaranteed by the Java language specification to never be null.
Certain expressions, such as the this keyword, object creation with new, literals (e.g., "hello", 42), and class literals (.class), can never evaluate to null. A check against null on any of these expressions is redundant and results in dead code that will never execute. This adds unnecessary clutter and can mislead developers into thinking the variable could be null under some circumstances.
Non-Compliant Code Examples
/**
* This class serves as a comprehensive non-compliant test case.
* Each method demonstrates a null check against an expression that is
* guaranteed by the language to be non-null. The static analysis rule
* should flag every "if" statement within this class.
*/publicclassUselessNullChecks{privateStringname;publicUselessNullChecks(Stringname){this.name=name;}// 1. Checking 'this'publicvoidcheckThisKeyword(){if(this==null){// Non-compliant: 'this' is never null.System.out.println("Error: 'this' is null.");}}// 2. Checking a 'new' object creation expressionpublicvoidcheckNewObject(){if(newUselessNullChecks("test")==null){// Non-compliant: 'new' returns a non-null instance or throws.System.out.println("Error: Object creation resulted in null.");}}// 3. Checking various literals that are never nullpublicvoidcheckLiterals(){if("some string"==null){// Non-compliant: String literalSystem.out.println("Error: String literal is null.");}if(42==null){// Non-compliant: Integer literalSystem.out.println("Error: Integer literal is null.");}if(true!=null){// Non-compliant: Boolean literalSystem.out.println("Boolean literal is not null.");}}}
Compliant Code Examples
/**
* This class serves as a comprehensive compliant test case.
* It showcases legitimate scenarios where a null check is necessary to
* prevent NullPointerExceptions. The static analysis rule should not
* flag any of the "if" statements within this class.
*/publicclassNecessaryNullChecks{privateStringvalue;publicNecessaryNullChecks(Stringvalue){this.value=value;}// A method that can return nullprivateStringfindProperty(Stringkey){if("name".equals(key)){returnthis.value;}returnnull;// A method can legitimately return null.}// 1. Checking a method parameter which could be nullpublicvoidprocessInput(Objectinput){if(input==null){// Compliant: Parameters can be null.System.out.println("Input was null, cannot proceed.");return;}System.out.println("Processing: "+input.toString());}// 2. Checking the result of a method call that may return nullpublicvoidcheckMethodResult(){Stringproperty=findProperty("address");if(property==null){// Compliant: The method is designed to return null.System.out.println("Property not found.");}else{System.out.println("Property length: "+property.length());}}// 3. Checking a field that can be nullpublicvoidcheckField(){// Create an instance where the field is nullNecessaryNullChecksinstance=newNecessaryNullChecks(null);if(instance.value==null){// Compliant: A class field can be null.System.out.println("The instance's value field is null.");}}}
Integraciones sin problemas. Prueba Datadog Code Security
Datadog Code Security
Prueba esta regla y analiza tu código con Datadog Code Security
Cómo usar esta regla
1
2
rulesets:- java-best-practices # Rules to enforce Java best practices.
Crea un static-analysis.datadog.yml con el contenido anterior en la raíz de tu repositorio
Utiliza nuestros complementos del IDE gratuitos o añade análisis de Code Security a tus pipelines de CI.