Add warning if we SET a variable that has already been set, but not used (if -du debug flag is set)

This commit is contained in:
Dianne Skoll
2025-05-24 23:06:50 -04:00
parent 9624b1045c
commit 09caa8988a
2 changed files with 14 additions and 1 deletions

View File

@@ -560,6 +560,8 @@ Var *FindVar(char const *str, int create)
v->v.type = INT_TYPE;
v->v.v.val = 0;
v->preserve = 0;
v->filename = GetCurrentFilename();
v->lineno = LineNo;
StrnCpy(v->name, str, VAR_NAME_LEN);
hash_table_insert(&VHashTbl, v);
@@ -593,8 +595,18 @@ int DeleteVar(char const *str)
/***************************************************************/
int SetVar(char const *str, Value const *val, int nonconst_expr)
{
Var *v = FindVar(str, 1);
Var *v = NULL;
if (DebugFlag & DB_UNUSED_VARS) {
v = FindVar(str, 0);
if (v && !(v->used_since_set)) {
Eprint(tr("Variable %s re-SET without being used (original SET was at %s:%d)"), str, v->filename, v->lineno);
}
}
if (!v) {
v = FindVar(str, 1);
}
if (!v) return E_NO_MEM; /* Only way FindVar can fail */
DestroyValue(v->v);

View File

@@ -24978,6 +24978,7 @@ TRANSLATE "Undefined %s function: `%s'" ""
TRANSLATE "Unmatched PUSH-OMIT-CONTEXT at %s(%d)" ""
TRANSLATE "Unrecognized command; interpreting as REM" ""
TRANSLATE "User function `%s' defined in non-constant context makes expression non-constant" ""
TRANSLATE "Variable %s re-SET without being used (original SET was at %s:%d)" ""
TRANSLATE "Variable assignment considered non-constant because of context" ""
TRANSLATE "Warning: Function name `%s...' truncated to `%s'" ""
TRANSLATE "Warning: OMIT is ignored if you use OMITFUNC" ""