Fix Autofix for LengthSubtractionCheck (#169)
This commit is contained in:
parent
f6c367c513
commit
8c61dc3cad
|
|
@ -5,8 +5,6 @@
|
||||||
|
|
||||||
module dscanner.analysis.length_subtraction;
|
module dscanner.analysis.length_subtraction;
|
||||||
|
|
||||||
import std.stdio;
|
|
||||||
|
|
||||||
import dscanner.analysis.base;
|
import dscanner.analysis.base;
|
||||||
import dscanner.analysis.helpers;
|
import dscanner.analysis.helpers;
|
||||||
|
|
||||||
|
|
@ -18,31 +16,35 @@ extern (C++) class LengthSubtractionCheck(AST) : BaseAnalyzerDmd
|
||||||
alias visit = BaseAnalyzerDmd.visit;
|
alias visit = BaseAnalyzerDmd.visit;
|
||||||
mixin AnalyzerInfo!"length_subtraction_check";
|
mixin AnalyzerInfo!"length_subtraction_check";
|
||||||
|
|
||||||
|
private enum KEY = "dscanner.suspicious.length_subtraction";
|
||||||
|
private enum MSG = "Avoid subtracting from '.length' as it may be unsigned.";
|
||||||
|
|
||||||
extern(D) this(string fileName)
|
extern(D) this(string fileName)
|
||||||
{
|
{
|
||||||
super(fileName);
|
super(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
override void visit(AST.BinExp be)
|
override void visit(AST.MinExp minExpr)
|
||||||
{
|
{
|
||||||
import dmd.tokens : EXP;
|
super.visit(minExpr);
|
||||||
|
|
||||||
if (auto de = be.e1.isDotIdExp())
|
auto left = minExpr.e1.isDotIdExp();
|
||||||
{
|
if (left is null || left.ident is null)
|
||||||
if (be.op == EXP.min && de.ident.toString() == "length")
|
return;
|
||||||
addErrorMessage(cast(size_t) de.loc.linnum, cast(size_t) de.loc.charnum + 1, KEY,
|
|
||||||
"Avoid subtracting from '.length' as it may be unsigned.");
|
|
||||||
}
|
|
||||||
|
|
||||||
super.visit(be);
|
if (left.ident.toString() == "length")
|
||||||
|
addErrorMessage(
|
||||||
|
cast(ulong) left.loc.linnum, cast(ulong) left.loc.charnum, KEY, MSG,
|
||||||
|
[AutoFix.insertionAt(minExpr.loc.fileOffset, "cast(ptrdiff_t) ")]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum KEY = "dscanner.suspicious.length_subtraction";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig;
|
import dscanner.analysis.config : Check, disabledConfig, StaticAnalysisConfig;
|
||||||
|
import dscanner.analysis.helpers : assertAnalyzerWarningsDMD, assertAutoFix;
|
||||||
|
import std.stdio : stderr;
|
||||||
|
|
||||||
StaticAnalysisConfig sac = disabledConfig();
|
StaticAnalysisConfig sac = disabledConfig();
|
||||||
sac.length_subtraction_check = Check.enabled;
|
sac.length_subtraction_check = Check.enabled;
|
||||||
|
|
@ -54,19 +56,19 @@ unittest
|
||||||
}
|
}
|
||||||
}c, sac);
|
}c, sac);
|
||||||
|
|
||||||
// TODO: Check and fix if broken
|
assertAutoFix(q{
|
||||||
//assertAutoFix(q{
|
void testSizeT()
|
||||||
//void testSizeT()
|
{
|
||||||
//{
|
if (i < a.length - 1) // fix
|
||||||
//if (i < a.length - 1) // fix
|
writeln("something");
|
||||||
//writeln("something");
|
}
|
||||||
//}
|
}c, q{
|
||||||
//}c, q{
|
void testSizeT()
|
||||||
//void testSizeT()
|
{
|
||||||
//{
|
if (i < cast(ptrdiff_t) a.length - 1) // fix
|
||||||
//if (i < cast(ptrdiff_t) a.length - 1) // fix
|
writeln("something");
|
||||||
//writeln("something");
|
}
|
||||||
//}
|
}c, sac, true);
|
||||||
//}c, sac);
|
|
||||||
stderr.writeln("Unittest for IfElseSameCheck passed.");
|
stderr.writeln("Unittest for IfElseSameCheck passed.");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue