Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jul 24, 2022
2 parents a8ff36b + a9c740b commit f9b01ba
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
19 changes: 19 additions & 0 deletions qulice-pmd/src/main/resources/com/qulice/pmd/ruleset.xml
Expand Up @@ -198,4 +198,23 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
</property>
</properties>
</rule>
<rule name="ReturnEmptyCollectionRatherThanNull" language="java" class="net.sourceforge.pmd.lang.rule.XPathRule" message="Returned null collection was found (NullCollection).">
<description>
Any method that returns an collection (i.e. an array or a List), it is better to return
an empty one rather than a null reference.
</description>
<priority>1</priority>
<properties>
<property name="version" value="2.0"/>
<!--Solve priority confict-->
<property name="xpath">
<value><![CDATA[
//MethodDeclaration[
(./ResultType/Type[pmd-java:typeIs('java.util.Collection')
or pmd-java:typeIs('java.util.Map') or @ArrayType=true()]) and
(./Block/BlockStatement/Statement/ReturnStatement/Expression/PrimaryExpression/PrimaryPrefix/Literal/NullLiteral)
]]]></value>
</property>
</properties>
</rule>
</ruleset>
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2011-2022 Qulice.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the Qulice.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.qulice.pmd;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

/**
* Test case for ReturnEmptyCollectionRatherThanNull.
*
* @since 0.19
*/
public class EmptyCollectionRuleTest {
/**
* Makes sure that empty collections not returned as null.
* @throws Exception when something goes wrong
*/
@Test
public void failsForNullCollection() throws Exception {
new PmdAssert(
"NullCollection.java",
Matchers.is(false),
Matchers.containsString(
"Returned null collection was found (NullCollection)."
)
).validate();
}
}
10 changes: 10 additions & 0 deletions qulice-pmd/src/test/resources/com/qulice/pmd/NullCollection.java
@@ -0,0 +1,10 @@
package emp;

import java.util.List;

class NullCollection {

public final List<Object> method() {
return null;
}
}

0 comments on commit f9b01ba

Please sign in to comment.