C++: Remove duplicate code queries that were deprecated over a year ago

This commit is contained in:
Jeroen Ketema
2022-03-08 12:28:41 +01:00
parent d2e2866276
commit df1e810f13
11 changed files with 0 additions and 391 deletions

View File

@@ -1,27 +0,0 @@
/**
* @deprecated
* @name Duplicate code
* @description This block of code is duplicated elsewhere. If possible, the shared code should be refactored so there is only one occurrence left. It may not always be possible to address these issues; other duplicate code checks (such as duplicate function, duplicate class) give subsets of the results with higher confidence.
* @kind problem
* @id cpp/duplicate-block
* @problem.severity recommendation
* @precision medium
* @tags testability
* maintainability
* duplicate-code
* non-attributable
*/
import CodeDuplication
from DuplicateBlock d, DuplicateBlock other, int lines, File otherFile, int otherLine
where
lines = d.sourceLines() and
lines > 10 and
other.getEquivalenceClass() = d.getEquivalenceClass() and
other != d and
otherFile = other.sourceFile() and
otherLine = other.sourceStartLine()
select d,
"Duplicate code: " + lines + " lines are duplicated at " + otherFile.getBaseName() + ":" +
otherLine

View File

@@ -1,43 +0,0 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>A function should never be duplicated verbatim in several places in the code. Of course
the severity of this anti-pattern is higher for longer functions than for extremely short
functions of one or two statements, but there are usually better ways of achieving the same
effect.</p>
</overview>
<recommendation>
<p>Code duplication in general is highly undesirable for a range of reasons: The artificially
inflated amount of code hinders comprehension, and ranges of similar but subtly different lines
can mask the real purpose or intention behind a function. There is also a risk of
update anomalies, where only one of several copies of the code is updated to address a defect or
add a feature.</p>
<p>In the case of function duplication, how to address the issue depends on the functions themselves
and on the precise classes in which the duplication occurs. At its simplest, the duplication can
be addressed by removing all but one of the duplicate function definitions and making
callers of the removed functions refer to the (now canonical) single remaining definition
instead.</p>
<p>This may not be possible for reasons of visibility or accessibility. A common example might
be where two classes implement the same functionality but neither is a subtype of the other,
so it is not possible to inherit a single function definition. In such cases, introducing a
common superclass to share the duplicated code is a viable option. Alternatively, if the functions
don't need access to private object state, they can be moved to a shared utility class that
just provides the functionality itself.</p>
</recommendation>
<references>
<li>Elmar Juergens, Florian Deissenboeck, Benjamin Hummel, and Stefan Wagner. 2009.
Do code clones matter? In <em>Proceedings of the 31st International Conference on
Software Engineering</em> (ICSE '09). IEEE Computer Society, Washington, DC, USA,
485-495.</li>
</references>
</qhelp>

View File

@@ -1,39 +0,0 @@
/**
* @deprecated
* @name Duplicate function
* @description There is another identical implementation of this function. Extract the code to a common file or superclass or delegate to improve sharing.
* @kind problem
* @id cpp/duplicate-function
* @problem.severity recommendation
* @precision medium
* @tags testability
* maintainability
* duplicate-code
* non-attributable
*/
import cpp
import CodeDuplication
predicate relevant(FunctionDeclarationEntry m) {
exists(Location loc |
loc = m.getBlock().getLocation() and
(
loc.getStartLine() + 5 < loc.getEndLine() and not m.getName().matches("get%")
or
loc.getStartLine() + 10 < loc.getEndLine()
)
)
}
from FunctionDeclarationEntry m, FunctionDeclarationEntry other
where
duplicateMethod(m, other) and
relevant(m) and
not m.getFunction().isConstructedFrom(_) and
not other.getFunction().isConstructedFrom(_) and
not fileLevelDuplication(m.getFile(), other.getFile()) and
not classLevelDuplication(m.getFunction().getDeclaringType(),
other.getFunction().getDeclaringType())
select m, "Function " + m.getName() + " is duplicated at $@.", other,
other.getFile().getBaseName() + ":" + other.getLocation().getStartLine().toString()

View File

@@ -1,45 +0,0 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>In cases where several methods are duplicated between two (or more) classes,
the classes themselves are highlighted as "mostly duplicate", rather than creating a large
number of method-level warnings. The same caveats apply here, too.</p>
</overview>
<recommendation>
<p>Code duplication in general is highly undesirable for a range of reasons: The artificially
inflated amount of code hinders comprehension, and ranges of similar but subtly different lines
can mask the real purpose or intention behind a method. There's also an omnipresent risk of
update anomalies, where only one of several copies of the code is updated to address a defect or
add a feature.</p>
<p>While completely duplicated classes are rare, they are usually a sign of a simple
oversight (or deliberate copy/paste) on a developer's part. Usually the required remedy
action is to remove all but one of them.</p>
<p>It is far more common to see duplication of many methods between two classes, leaving just
a few that are actually different. Such situations warrant close inspection. Are the differences
deliberate or a result of an inconsistent update to one of the clones? If the latter, then
treating the classes as completely duplicate and eliminating all but one (while preserving any
corrections or new features that may have been introduced) is the best course. If two classes serve
genuinely different purposes but almost all of their methods are the same, that can be a sign
that there is a missing level of abstraction. Introducing a common superclass to define the
common methods and sharing the code is likely to prevent many problems in the long term.</p>
<p>Modern IDEs may provide refactoring support for this sort of transformation, usually
under the names of "Pull up" or "Extract supertype".</p>
</recommendation>
<references>
<li>Elmar Juergens, Florian Deissenboeck, Benjamin Hummel, and Stefan Wagner. 2009.
Do code clones matter? In <em>Proceedings of the 31st International Conference on
Software Engineering</em> (ICSE '09). IEEE Computer Society, Washington, DC, USA,
485-495.</li>
</references>
</qhelp>

View File

@@ -1,24 +0,0 @@
/**
* @deprecated
* @name Mostly duplicate class
* @description More than 80% of the methods in this class are duplicated in another class. Create a common supertype to improve code sharing.
* @kind problem
* @id cpp/duplicate-class
* @problem.severity recommendation
* @precision medium
* @tags testability
* maintainability
* duplicate-code
* non-attributable
*/
import cpp
import CodeDuplication
from Class c, Class other, string message
where
mostlyDuplicateClass(c, other, message) and
not c.isConstructedFrom(_) and
not other.isConstructedFrom(_) and
not fileLevelDuplication(c.getFile(), _)
select c, message, other, other.getQualifiedName()

View File

@@ -1,46 +0,0 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>In cases where most of a file's lines have been duplicated in one or more other
files, the files themselves are highlighted as "mostly duplicate", rather than
creating a large number of method-level or class-level warnings. The same caveats
apply here, too.</p>
</overview>
<recommendation>
<p>Code duplication in general is highly undesirable for a range of reasons: The artificially
inflated amount of code hinders comprehension, and ranges of similar but subtly different lines
can mask the real purpose or intention behind a method. There's also an omnipresent risk of
update anomalies, where only one of several copies of the code is updated to address a defect or
add a feature.</p>
<p>While completely duplicated files are rare, they are usually a sign of a simple
oversight (or deliberate copy/paste) on a developer's part. Usually the required remedy
action is to remove all but one of them. A common exception may arise from generated code
that simply occurs in several places in the source tree; the check can be adapted to
exclude such results.</p>
<p>It is far more common to see duplication of many lines between two files, leaving just
a few that are actually different. Such situations warrant close inspection. Are the differences
deliberate or a result of an inconsistent update to one of the clones? If the latter, then
treating the files as completely duplicate and eliminating all but one (while preserving any
corrections or new features that may have been introduced) is the best course. If two files serve
genuinely different purposes but almost all of their lines are the same, that can be a sign
that there is a missing level of abstraction. Look for ways to share the functionality, either
by extracting a utility class for parts of it or by encapsulating the common parts into a new
super class of any classes involved.</p>
</recommendation>
<references>
<li>Elmar Juergens, Florian Deissenboeck, Benjamin Hummel, and Stefan Wagner. 2009.
Do code clones matter? In <em>Proceedings of the 31st International Conference on
Software Engineering</em> (ICSE '09). IEEE Computer Society, Washington, DC, USA,
485-495.</li>
</references>
</qhelp>

View File

@@ -1,21 +0,0 @@
/**
* @deprecated
* @name Mostly duplicate file
* @description There is another file that shares a lot of the code with this file. Merge the two files to improve maintainability.
* @kind problem
* @id cpp/duplicate-file
* @problem.severity recommendation
* @precision medium
* @tags testability
* maintainability
* duplicate-code
* non-attributable
*/
import cpp
import CodeDuplication
from File f, File other, int percent
where duplicateFiles(f, other, percent)
select f, percent + "% of the lines in " + f.getBaseName() + " are copies of lines in $@.", other,
other.getBaseName()

View File

@@ -1,55 +0,0 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>A "mostly duplicate function" is a function for which there is at least one
almost exact duplicate somewhere else in the code, but for which there are no
exact duplicates. There will be minor typographical differences between this
function and any "mostly duplicate function" to which it corresponds (for
example, comments and small code changes), preventing an exact match. Pairs of
such functions are sometimes referred to as "similar".</p>
<p>This class of problem can often be more insidious than mere duplication, because the two
implementations have diverged. This may be on purpose (when a function is copy-and-pasted
and adapted to a new context) or accidentally (when a correction is only introduced in one of
several identical pieces of code), and to address the problem one needs to understand which
of the two situations applies.</p>
</overview>
<recommendation>
<p>Code duplication in general is highly undesirable for a range of reasons: The artificially
inflated amount of code hinders comprehension, and ranges of similar but subtly different lines
can mask the real purpose or intention behind a function. There's also a risk of
update anomalies, where only one of several copies of the code is updated to address a defect or
add a feature.</p>
<p>In the case of function similarity, how to address the issue depends on the functions
themselves and on the precise classes in which they occur. At its simplest, if the differences
are accidental, the problem can be addressed by unifying the functions to behave identically.
Then, we can remove all but one of the duplicate function definitions and make
callers of the removed functions refer to the (now canonical) single remaining definition
instead.</p>
<p>In more complex cases, look for ways of encapsulating the commonality and sharing it while
retaining the differences in functionality. Perhaps the function can be moved to a single place
and given an additional parameter, allowing it to cover all use cases? Alternatively, there
may be a common preprocessing or postprocessing step which can be extracted to its own (shared)
function, leaving only the specific parts in the existing functions.</p>
<p>Modern IDEs may provide refactoring support for this sort of transformation. Relevant
refactorings might be "Extract function", "Change function signature", "Pull up" or "Extract
supertype".</p>
</recommendation>
<references>
<li>Elmar Juergens, Florian Deissenboeck, Benjamin Hummel, and Stefan Wagner. 2009.
Do code clones matter? In <em>Proceedings of the 31st International Conference on
Software Engineering</em> (ICSE '09). IEEE Computer Society, Washington, DC, USA,
485-495.</li>
</references>
</qhelp>

View File

@@ -1,31 +0,0 @@
/**
* @deprecated
* @name Mostly duplicate function
* @description There is another function that shares a lot of the code with this one. Extract the code to a common file/superclass or delegate to improve sharing.
* @kind problem
* @id cpp/mostly-duplicate-function
* @problem.severity recommendation
* @precision medium
* @tags testability
* duplicate-code
* non-attributable
*/
import cpp
import CodeDuplication
from FunctionDeclarationEntry m, int covered, int total, FunctionDeclarationEntry other, int percent
where
duplicateStatements(m, other, covered, total) and
covered != total and
total > 5 and
covered * 100 / total = percent and
percent > 80 and
not m.getFunction().isConstructedFrom(_) and
not other.getFunction().isConstructedFrom(_) and
not duplicateMethod(m, other) and
not classLevelDuplication(m.getFunction().getDeclaringType(),
other.getFunction().getDeclaringType()) and
not fileLevelDuplication(m.getFile(), other.getFile())
select m, percent + "% of the statements in " + m.getName() + " are duplicated in $@.", other,
other.getFunction().getQualifiedName()

View File

@@ -1,39 +0,0 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Two lines are defined as similar if they are either identical or contain only very minor
differences. In cases where most of the lines in a file have corresponding "similar" lines in another file,
the files themselves are highlighted as "mostly similar", instead of creating a large number of method-level or class-level
warnings. The same caveats apply here, too.</p>
</overview>
<recommendation>
<p>Code duplication in general is highly undesirable for a range of reasons: The artificially
inflated amount of code hinders comprehension, and ranges of similar but subtly different lines
can mask the real purpose or intention behind a method. There's also an omnipresent risk of
update anomalies, where only one of several copies of the code is updated to address a defect or
add a feature.</p>
<p>With files that are marked as mostly similar, special care should be taken. Why are
they almost the same, and why are there differences? If the differences are accidental (for
example from corrections that were only applied to one copy), then unifying the files and removing
all but one is the best thing to do. If the files have genuinely different tasks, it is worth
thinking about the reasons for the similarity. Can some of the shared code be extracted into
methods (perhaps with additional parameters, to cover the differences in behavior)? Should it
be moved into a utility class or file that is accessible to all current implementations, or
should a new level of abstraction be introduced?</p>
</recommendation>
<references>
<li>Elmar Juergens, Florian Deissenboeck, Benjamin Hummel, and Stefan Wagner. 2009.
Do code clones matter? In <em>Proceedings of the 31st International Conference on
Software Engineering</em> (ICSE '09). IEEE Computer Society, Washington, DC, USA,
485-495.</li>
</references>
</qhelp>

View File

@@ -1,21 +0,0 @@
/**
* @deprecated
* @name Mostly similar file
* @description There is another file that shares a lot of the code with this file. Notice that names of variables and types may have been changed. Merge the two files to improve maintainability.
* @kind problem
* @id cpp/similar-file
* @problem.severity recommendation
* @precision medium
* @tags testability
* maintainability
* duplicate-code
* non-attributable
*/
import cpp
import CodeDuplication
from File f, File other, int percent
where similarFiles(f, other, percent)
select f, percent + "% of the lines in " + f.getBaseName() + " are similar to lines in $@.", other,
other.getBaseName()