Skip to content

Cleanup/assert correctness and formatting#12972

Open
JohnsterID wants to merge 2 commits into
LoneGazebo:masterfrom
JohnsterID:cleanup/assert-correctness-and-formatting
Open

Cleanup/assert correctness and formatting#12972
JohnsterID wants to merge 2 commits into
LoneGazebo:masterfrom
JohnsterID:cleanup/assert-correctness-and-formatting

Conversation

@JohnsterID

Copy link
Copy Markdown
Contributor

Better to place if/else/for/while bodies on separate lines from the condition to allow precise debugger breakpoint placement.

@azum4roll

azum4roll commented May 11, 2026

Copy link
Copy Markdown
Collaborator

Note that we may turn off PRECONDITIONs and ASSERTs for stable versions. In that case, those do nothing.

I'm against turning them off though.

@JohnsterID

JohnsterID commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

I would be against blanket disabling. ASSERT (always active in debug/release when CVASSERT_ENABLE is defined so we could switch off in release builds) but PRECONDITIONs should never be turned off.

Remove 98 dead guard clauses after PRECONDITION:
PRECONDITION crashes via BUILTIN_TRAP() on failure, making any guard
clause that checks the same condition unreachable dead code.
- CvMinorCivAI.cpp (69), CvVotingClasses.cpp (6), CvPlot.cpp (5),
  CvUnit.cpp (4), CvGlobals.cpp (4), CvCultureClasses.cpp (4),
  CvGame.cpp (2), CvTeam.cpp (2), CvCity.cpp (1), CvTechClasses.cpp (1)

Add ASSERT before 58 internal-only bounds-check guard clauses:
Per-instance analysis classified each guard as Lua-exposed (guard-only
correct), internal (ASSERT + guard needed), or business logic (no
macro needed). ASSERT provides debug visibility while the guard clause
preserves safe runtime recovery.
- CvDiplomacyAI.cpp (26), CvMinorCivAI.cpp (25), CvPlayer.cpp (4),
  CvVotingClasses.cpp (2), CvTeam.cpp (1)

Fix 3 PRECONDITION misuses where null IS a possible runtime state:
- CvTacticalAI.cpp (2): getBestDefender() can return null between
  isEnemyUnit check and lookup (unit killed during AI turn).
  PRECONDITION would crash; now ASSERT + guard to continue safely.
- CvReligionClasses.cpp (1): getReligionInfo() can return null for
  invalid religion (corrupted save, removed mod). The function already
  had a fallback ('No Religion') that was unreachable due to
  PRECONDITION crash. Now ASSERT allows the fallback to work.
Place if/else/for/while bodies on separate lines from the condition
to allow precise debugger breakpoint placement.

550 formatting fixes across 52 files:
- CvGameCoreDLL_Expansion2: 37 files, 374 fixes
- FirePlace: 11 files, 158 fixes
- CvWorldBuilderMap: 4 files, 18 fixes

All changes are whitespace-only — zero functional impact.
Compiler generates identical code.
@JohnsterID JohnsterID force-pushed the cleanup/assert-correctness-and-formatting branch from 9f56ae5 to 70961f4 Compare June 13, 2026 04:41
@JohnsterID

Copy link
Copy Markdown
Contributor Author

Conflicts resolved.

if ( !_stricmp(szType, "String"))
return FVARTYPE_STRING;
if ( !_stricmp(szType, "String"))
return FVARTYPE_WSTRING;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between those?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants