Skip to content

Commit

Permalink
Fix Autofix for DeleteCheck (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladiwostok authored Oct 3, 2024
1 parent 51a1ef5 commit 96115a6
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/dscanner/analysis/del.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,42 @@ import dsymbol.scope_;
*/
extern(C++) class DeleteCheck(AST) : BaseAnalyzerDmd
{
// alias visit = BaseAnalyzerDmd!AST.visit;
alias visit = BaseAnalyzerDmd.visit;
mixin AnalyzerInfo!"delete_check";

private enum KEY = "dscanner.deprecated.delete_keyword";
private enum MSG = "Avoid using the 'delete' keyword.";

extern(D) this(string fileName)
{
super(fileName);
}

override void visit(AST.DeleteExp d)
{
addErrorMessage(cast(ulong) d.loc.linnum, cast(ulong) d.loc.charnum, "dscanner.deprecated.delete_keyword",
"Avoid using the 'delete' keyword.");
import dmd.hdrgen : toChars;
import std.conv : to;

string exprStr = to!string(toChars(d));

addErrorMessage(
cast(ulong) d.loc.linnum, cast(ulong) d.loc.charnum, KEY, MSG,
[AutoFix.replacement(d.loc.fileOffset, d.loc.fileOffset + 6, `destroy(`, "Replace delete with destroy()")
.concat(AutoFix.insertionAt(d.loc.fileOffset + exprStr.length, ")"))]
);

super.visit(d);
}
}

unittest
{
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig;
import dscanner.analysis.helpers : assertAnalyzerWarnings;
import dscanner.analysis.helpers : assertAnalyzerWarnings, assertAutoFix;

StaticAnalysisConfig sac = disabledConfig();
sac.delete_check = Check.enabled;

assertAnalyzerWarningsDMD(q{
void testDelete()
{
Expand All @@ -50,5 +62,25 @@ unittest
}
}c, sac);

assertAutoFix(q{
void testDelete()
{
int[int] data = [1 : 2];
delete data[1]; // fix

auto a = new Class();
delete a; // fix
}
}c, q{
void testDelete()
{
int[int] data = [1 : 2];
destroy(data[1]); // fix

auto a = new Class();
destroy(a); // fix
}
}c, sac, true);

stderr.writeln("Unittest for DeleteCheck passed.");
}

0 comments on commit 96115a6

Please sign in to comment.