Skip to content

Commit

Permalink
make mismatched args not warn on named arguments
Browse files Browse the repository at this point in the history
makes expressions be assumed as valid argument names
  • Loading branch information
WebFreak001 committed May 20, 2023
1 parent 49ede79 commit b213aee
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/dscanner/analysis/mismatched_args.d
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ final class ArgVisitor : ASTVisitor
foreach (a; al.items)
{
auto u = cast(UnaryExpression) a.assignExpression;
if (u !is null)
size_t prevArgs = args.length;
if (u !is null && !a.name.text.length)
visit(u);
else

if (args.length == prevArgs)
{
// if we didn't get an identifier in the unary expression,
// assume it's a good argument
args ~= istring.init;
lines ~= size_t.max;
columns ~= size_t.max;
lines ~= a.tokens.length ? a.tokens[0].line : size_t.max;
columns ~= a.tokens.length ? a.tokens[0].column : size_t.max;
}
}
}
Expand Down Expand Up @@ -249,3 +253,33 @@ unittest
assert(res == []);
}
}

unittest
{
import dscanner.analysis.helpers : assertAnalyzerWarnings;
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig;
import std.stdio : stderr;

StaticAnalysisConfig sac = disabledConfig();
sac.mismatched_args_check = Check.enabled;
assertAnalyzerWarnings(q{
void foo(int x, int y)
{
}

void bar()
{
int x = 1;
int y = 2;
foo(y, x); // [warn]: Argument 2 is named 'x', but this is the name of parameter 1
foo(y + 1, x); // [warn]: Argument 2 is named 'x', but this is the name of parameter 1
foo(y + 1, f(x));
foo(x: y, y: x);

// foo(y: y, x); // TODO: this shouldn't error
foo(x, y: x); // TODO: this should error
foo(y, y: x); // [warn]: Argument 1 is named 'y', but this is the name of parameter 2
}
}c, sac);
stderr.writeln("Unittest for MismatchedArgumentCheck passed.");
}

0 comments on commit b213aee

Please sign in to comment.