Add check for variables which are both shadowing and unused by default

This commit is contained in:
gingerBill
2022-02-05 14:31:22 +00:00
parent dd84b61cc8
commit cf246f65ff
+18 -9
View File
@@ -504,6 +504,7 @@ enum VettedEntityKind {
VettedEntity_Unused, VettedEntity_Unused,
VettedEntity_Shadowed, VettedEntity_Shadowed,
VettedEntity_Shadowed_And_Unused,
}; };
struct VettedEntity { struct VettedEntity {
VettedEntityKind kind; VettedEntityKind kind;
@@ -625,12 +626,18 @@ void check_scope_usage(Checker *c, Scope *scope) {
MUTEX_GUARD_BLOCK(scope->mutex) for_array(i, scope->elements.entries) { MUTEX_GUARD_BLOCK(scope->mutex) for_array(i, scope->elements.entries) {
Entity *e = scope->elements.entries[i].value; Entity *e = scope->elements.entries[i].value;
if (e == nullptr) continue; if (e == nullptr) continue;
VettedEntity ve = {}; VettedEntity ve_unused = {};
if (vet_unused && check_vet_unused(c, e, &ve)) { VettedEntity ve_shadowed = {};
array_add(&vetted_entities, ve); bool is_unused = vet_unused && check_vet_unused(c, e, &ve_unused);
} bool is_shadowed = vet_shadowing && check_vet_shadowing(c, e, &ve_shadowed);
if (vet_shadowing && check_vet_shadowing(c, e, &ve)) { if (is_unused && is_shadowed) {
array_add(&vetted_entities, ve); VettedEntity ve_both = ve_shadowed;
ve_both.kind = VettedEntity_Shadowed_And_Unused;
array_add(&vetted_entities, ve_both);
} else if (is_unused) {
array_add(&vetted_entities, ve_unused);
} else if (is_shadowed) {
array_add(&vetted_entities, ve_shadowed);
} }
} }
@@ -642,16 +649,18 @@ void check_scope_usage(Checker *c, Scope *scope) {
Entity *other = ve.other; Entity *other = ve.other;
String name = e->token.string; String name = e->token.string;
if (build_context.vet) { if (ve.kind == VettedEntity_Shadowed_And_Unused) {
error(e->token, "'%.*s' declared but not used, possibly shadows declaration at line %d", LIT(name), other->token.pos.line);
} else if (build_context.vet) {
switch (ve.kind) { switch (ve.kind) {
case VettedEntity_Unused: case VettedEntity_Unused:
error(e->token, "'%.*s' declared but not used", LIT(name)); error(e->token, "'%.*s' declared but not used", LIT(name));
break; break;
case VettedEntity_Shadowed: case VettedEntity_Shadowed:
if (e->flags&EntityFlag_Using) { if (e->flags&EntityFlag_Using) {
error(e->token, "Declaration of '%.*s' from 'using' shadows declaration at line %lld", LIT(name), cast(long long)other->token.pos.line); error(e->token, "Declaration of '%.*s' from 'using' shadows declaration at line %d", LIT(name), other->token.pos.line);
} else { } else {
error(e->token, "Declaration of '%.*s' shadows declaration at line %lld", LIT(name), cast(long long)other->token.pos.line); error(e->token, "Declaration of '%.*s' shadows declaration at line %d", LIT(name), other->token.pos.line);
} }
break; break;
default: default: