Skip to content

Commit

Permalink
Clearify key names
Browse files Browse the repository at this point in the history
  • Loading branch information
kubo39 authored and WebFreak001 committed May 6, 2024
1 parent 433d1eb commit 17f3286
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/dscanner/analysis/asm_style.d
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ final class AsmStyleCheck : BaseAnalyzer
if (brExp.asmBrExp !is null && brExp.asmBrExp.asmUnaExp !is null
&& brExp.asmBrExp.asmUnaExp.asmPrimaryExp !is null)
{
addErrorMessage(brExp, "dscanner.confusing.brexp",
addErrorMessage(brExp, KEY,
"This is confusing because it looks like an array index. Rewrite a[1] as [a + 1] to clarify.");
}
brExp.accept(this);
}

private enum string KEY = "dscanner.confusing.brexp";
}

unittest
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/del.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ final class DeleteCheck : BaseAnalyzer

override void visit(const DeleteExpression d)
{
addErrorMessage(d.tokens[0], "dscanner.deprecated.delete_keyword",
addErrorMessage(d.tokens[0], KEY,
"Avoid using the 'delete' keyword.",
[AutoFix.replacement(d.tokens[0], `destroy(`, "Replace delete with destroy()")
.concat(AutoFix.insertionAfter(d.tokens[$ - 1], ")"))]);
d.accept(this);
}

private enum string KEY = "dscanner.deprecated.delete_keyword";
}

unittest
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/duplicate_attribute.d
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ final class DuplicateAttributeCheck : BaseAnalyzer
if (hasAttribute)
{
string message = "Attribute '%s' is duplicated.".format(attributeName);
addErrorMessage(tokens, "dscanner.unnecessary.duplicate_attribute", message,
addErrorMessage(tokens, KEY, message,
[AutoFix.replacement(tokens, "", "Remove second attribute " ~ attributeName)]);
}

Expand Down Expand Up @@ -149,6 +149,8 @@ final class DuplicateAttributeCheck : BaseAnalyzer

return null;
}

private enum string KEY = "dscanner.unnecessary.duplicate_attribute";
}

unittest
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/enumarrayliteral.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class EnumArrayLiteralCheck : BaseAnalyzer
if (part.initializer.nonVoidInitializer.arrayInitializer is null)
continue;
addErrorMessage(part.initializer.nonVoidInitializer,
"dscanner.performance.enum_array_literal",
KEY,
"This enum may lead to unnecessary allocation at run-time."
~ " Use 'static immutable "
~ part.identifier.text ~ " = [ ...' instead.",
Expand All @@ -58,6 +58,8 @@ final class EnumArrayLiteralCheck : BaseAnalyzer
}
autoDec.accept(this);
}

private enum string KEY = "dscanner.performance.enum_array_literal";
}

unittest
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/has_public_example.d
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ final class HasPublicExampleCheck : BaseAnalyzer

private:

enum string KEY = "dscanner.style.has_public_example";

bool hasDitto(Decl)(const Decl decl)
{
import ddoc.comments : parseComment;
Expand Down Expand Up @@ -164,7 +166,7 @@ private:
{
import std.string : format;

addErrorMessage(tokens, "dscanner.style.has_public_example", name is null
addErrorMessage(tokens, KEY, name is null
? "Public declaration has no documented example."
: format("Public declaration '%s' has no documented example.", name));
}
Expand Down
14 changes: 10 additions & 4 deletions src/dscanner/analysis/ifelsesame.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class IfElseSameCheck : BaseAnalyzer
// extend 1 past, so we include the `else` token
tokens = (tokens.ptr - 1)[0 .. tokens.length + 1];
addErrorMessage(tokens,
"dscanner.bugs.if_else_same", "'Else' branch is identical to 'Then' branch.");
IF_ELSE_SAME_KEY, "'Else' branch is identical to 'Then' branch.");
}
ifStatement.accept(this);
}
Expand All @@ -50,7 +50,7 @@ final class IfElseSameCheck : BaseAnalyzer
if (e !is null && assignExpression.operator == tok!"="
&& e.ternaryExpression == assignExpression.ternaryExpression)
{
addErrorMessage(assignExpression, "dscanner.bugs.self_assignment",
addErrorMessage(assignExpression, SELF_ASSIGNMENT_KEY,
"Left side of assignment operatior is identical to the right side.");
}
assignExpression.accept(this);
Expand All @@ -62,7 +62,7 @@ final class IfElseSameCheck : BaseAnalyzer
&& andAndExpression.left == andAndExpression.right)
{
addErrorMessage(andAndExpression.right,
"dscanner.bugs.logic_operator_operands",
LOGIC_OPERATOR_OPERANDS_KEY,
"Left side of logical and is identical to right side.");
}
andAndExpression.accept(this);
Expand All @@ -74,11 +74,17 @@ final class IfElseSameCheck : BaseAnalyzer
&& orOrExpression.left == orOrExpression.right)
{
addErrorMessage(orOrExpression.right,
"dscanner.bugs.logic_operator_operands",
LOGIC_OPERATOR_OPERANDS_KEY,
"Left side of logical or is identical to right side.");
}
orOrExpression.accept(this);
}

private:

enum string IF_ELSE_SAME_KEY = "dscanner.bugs.if_else_same";
enum string SELF_ASSIGNMENT_KEY = "dscanner.bugs.self_assignment";
enum string LOGIC_OPERATOR_OPERANDS_KEY = "dscanner.bugs.logic_operator_operands";
}

unittest
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/label_var_same_name_check.d
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ final class LabelVarNameCheck : ScopedBaseAnalyzer

private:

enum string KEY = "dscanner.suspicious.label_var_same_name";

Thing[string][] stack;

template AggregateVisit(NodeType)
Expand Down Expand Up @@ -88,7 +90,7 @@ private:
{
immutable thisKind = fromLabel ? "Label" : "Variable";
immutable otherKind = thing.isVar ? "variable" : "label";
addErrorMessage(name, "dscanner.suspicious.label_var_same_name",
addErrorMessage(name, KEY,
thisKind ~ " \"" ~ fqn ~ "\" has the same name as a "
~ otherKind ~ " defined on line " ~ to!string(thing.line) ~ ".");
}
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/length_subtraction.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import dsymbol.scope_;
*/
final class LengthSubtractionCheck : BaseAnalyzer
{
private enum string KEY = "dscanner.suspicious.length_subtraction";

alias visit = BaseAnalyzer.visit;

mixin AnalyzerInfo!"length_subtraction_check";
Expand All @@ -40,7 +42,7 @@ final class LengthSubtractionCheck : BaseAnalyzer
if (l.identifierOrTemplateInstance is null
|| l.identifierOrTemplateInstance.identifier.text != "length")
goto end;
addErrorMessage(addExpression, "dscanner.suspicious.length_subtraction",
addErrorMessage(addExpression, KEY,
"Avoid subtracting from '.length' as it may be unsigned.",
[
AutoFix.insertionBefore(l.tokens[0], "cast(ptrdiff_t) ", "Cast to ptrdiff_t")
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/local_imports.d
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class LocalImportCheck : BaseAnalyzer
if (singleImport.rename.text.length == 0)
{
addErrorMessage(singleImport,
"dscanner.suspicious.local_imports", "Local imports should specify"
KEY, "Local imports should specify"
~ " the symbols being imported to avoid hiding local symbols.");
}
}
Expand All @@ -68,6 +68,8 @@ final class LocalImportCheck : BaseAnalyzer

private:

enum string KEY = "dscanner.suspicious.local_imports";

mixin template visitThing(T)
{
override void visit(const T thing)
Expand Down
5 changes: 4 additions & 1 deletion src/dscanner/analysis/numbers.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ public:
&& ((t.text.startsWith("0b") && !t.text.matchFirst(badBinaryRegex)
.empty) || !t.text.matchFirst(badDecimalRegex).empty))
{
addErrorMessage(t, "dscanner.style.number_literals",
addErrorMessage(t, KEY,
"Use underscores to improve number constant readability.");
}
}

private:

enum string KEY = "dscanner.style.number_literals";

auto badBinaryRegex = ctRegex!(`^0b[01]{9,}`);
auto badDecimalRegex = ctRegex!(`^\d{5,}`);
}
Expand Down
15 changes: 9 additions & 6 deletions src/dscanner/analysis/objectconst.d
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class ObjectConstCheck : BaseAnalyzer
if (inAggregate && !constColon && !constBlock && !isDeclationDisabled
&& isInteresting(fd.name.text) && !hasConst(fd.memberFunctionAttributes))
{
addErrorMessage(d.functionDeclaration.name, "dscanner.suspicious.object_const",
addErrorMessage(d.functionDeclaration.name, KEY,
"Methods 'opCmp', 'toHash', 'opEquals', 'opCast', and/or 'toString' are non-const.");
}
}
Expand All @@ -81,23 +81,26 @@ final class ObjectConstCheck : BaseAnalyzer
constBlock = false;
}

private static bool hasConst(const MemberFunctionAttribute[] attributes)
private:

enum string KEY = "dscanner.suspicious.object_const";

static bool hasConst(const MemberFunctionAttribute[] attributes)
{
import std.algorithm : any;

return attributes.any!(a => a.tokenType == tok!"const"
|| a.tokenType == tok!"immutable" || a.tokenType == tok!"inout");
}

private static bool isInteresting(string name)
static bool isInteresting(string name)
{
return name == "opCmp" || name == "toHash" || name == "opEquals"
|| name == "toString" || name == "opCast";
}

private bool constBlock;
private bool constColon;

bool constBlock;
bool constColon;
}

unittest
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/redundant_storage_class.d
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ final class RedundantStorageClassCheck : BaseAnalyzer
return;
auto t = vd.declarators[0].name;
string message = REDUNDANT_VARIABLE_ATTRIBUTES.format(t.text, globalAttributes);
addErrorMessage(t, "dscanner.unnecessary.duplicate_attribute", message);
addErrorMessage(t, KEY, message);
}
}

private enum string KEY = "dscanner.unnecessary.duplicate_attribute";
}

unittest
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/undocumented.d
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ final class UndocumentedDeclarationCheck : BaseAnalyzer

private:

enum string KEY = "dscanner.style.undocumented_declaration";

mixin template V(T)
{
override void visit(const T declaration)
Expand Down Expand Up @@ -223,7 +225,7 @@ private:
{
import std.string : format;

addErrorMessage(range, "dscanner.style.undocumented_declaration", name is null
addErrorMessage(range, KEY, name is null
? "Public declaration is undocumented."
: format("Public declaration '%s' is undocumented.", name));
}
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/unmodified.d
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ final class UnmodifiedFinder : BaseAnalyzer

private:

enum string KEY = "dscanner.suspicious.unmodified";

template PartsMightModify(T)
{
override void visit(const T t)
Expand Down Expand Up @@ -300,7 +302,7 @@ private:
{
immutable string errorMessage = "Variable " ~ vi.name
~ " is never modified and could have been declared const or immutable.";
addErrorMessage(vi.token, "dscanner.suspicious.unmodified", errorMessage);
addErrorMessage(vi.token, KEY, errorMessage);
}
tree = tree[0 .. $ - 1];
}
Expand Down
4 changes: 3 additions & 1 deletion src/dscanner/analysis/unused_label.d
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ final class UnusedLabelCheck : BaseAnalyzer

private:

enum string KEY = "dscanner.suspicious.unused_label";

static struct Label
{
string name;
Expand Down Expand Up @@ -144,7 +146,7 @@ private:
}
else if (!label.used)
{
addErrorMessage(label.token, "dscanner.suspicious.unused_label",
addErrorMessage(label.token, KEY,
"Label \"" ~ label.name ~ "\" is not used.");
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/dscanner/analysis/useless_initializer.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class UselessInitializerChecker : BaseAnalyzer

private:

enum key = "dscanner.useless-initializer";
enum string KEY = "dscanner.useless-initializer";

version(unittest)
{
Expand Down Expand Up @@ -161,15 +161,15 @@ public:
{
void warn(const BaseNode range)
{
addErrorMessage(range, key, msg);
addErrorMessage(range, KEY, msg);
}
}
else
{
import std.format : format;
void warn(const BaseNode range)
{
addErrorMessage(range, key, msg.format(declarator.name.text));
addErrorMessage(range, KEY, msg.format(declarator.name.text));
}
}

Expand Down

0 comments on commit 17f3286

Please sign in to comment.