Add nil check to FactoryUnit OnStopBuild#7029
Conversation
📝 WalkthroughWalkthroughThis PR fixes a nil value error in FactoryUnit.OnStopBuild by adding a guard check to ensure Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@lua/sim/units/FactoryUnit.lua`:
- Around line 140-143: The current block calls IsDestroyed(unitBeingBuilt)
without first ensuring unitBeingBuilt is non-nil; update the conditional to
first check unitBeingBuilt before calling IsDestroyed and GetFractionComplete so
it follows the project's nil-safe pattern (e.g., guard with "if unitBeingBuilt
and not IsDestroyed(unitBeingBuilt) and unitBeingBuilt:GetFractionComplete() < 1
then unitBeingBuilt:Destroy() end"). Modify the section that references
unitBeingBuilt, IsDestroyed, GetFractionComplete, and Destroy to perform the
explicit nil check first.
| -- Factory can stop building but still have an unbuilt unit if a mobile build order is issued and the order is cancelled | ||
| if unitBeingBuilt:GetFractionComplete() < 1 then | ||
| if not IsDestroyed(unitBeingBuilt) and unitBeingBuilt:GetFractionComplete() < 1 then | ||
| unitBeingBuilt:Destroy() | ||
| end |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# Find the IsDestroyed function definition
rg -n 'IsDestroyed\s*=' --type lua -A 10 | head -50Repository: FAForever/fa
Length of output: 4372
🏁 Script executed:
# Also search in Core.lua or where it's typically defined
fd -e lua | xargs grep -l 'IsDestroyed' | head -10Repository: FAForever/fa
Length of output: 957
🏁 Script executed:
# Check usage of IsDestroyed with potentially nil values
rg -n 'IsDestroyed\(' --type lua -B 2 -A 2 | head -100Repository: FAForever/fa
Length of output: 7110
🏁 Script executed:
# Search for IsDestroyed function definition in engine files
rg -n 'function IsDestroyed' --type lua -A 5Repository: FAForever/fa
Length of output: 270
🏁 Script executed:
# Check the FactoryUnit.lua file to see both line 102 and 140-143
cat lua/sim/units/FactoryUnit.lua | head -150 | tail -60Repository: FAForever/fa
Length of output: 2424
The fix requires an explicit nil check before IsDestroyed().
The codebase consistently uses patterns like if value and not IsDestroyed(value) or if (not value) or IsDestroyed(value) to guard against nil. Since IsDestroyed is an engine function (implemented in C++, not Lua), it does not safely handle nil values. The current fix at lines 140-143 violates this pattern and risks the same nil-related crash if unitBeingBuilt is nil.
The fix should be:
- if not IsDestroyed(unitBeingBuilt) and unitBeingBuilt:GetFractionComplete() < 1 then
+ if unitBeingBuilt and not IsDestroyed(unitBeingBuilt) and unitBeingBuilt:GetFractionComplete() < 1 then🤖 Prompt for AI Agents
In `@lua/sim/units/FactoryUnit.lua` around lines 140 - 143, The current block
calls IsDestroyed(unitBeingBuilt) without first ensuring unitBeingBuilt is
non-nil; update the conditional to first check unitBeingBuilt before calling
IsDestroyed and GetFractionComplete so it follows the project's nil-safe pattern
(e.g., guard with "if unitBeingBuilt and not IsDestroyed(unitBeingBuilt) and
unitBeingBuilt:GetFractionComplete() < 1 then unitBeingBuilt:Destroy() end").
Modify the section that references unitBeingBuilt, IsDestroyed,
GetFractionComplete, and Destroy to perform the explicit nil check first.
Description of the proposed changes
Fixes this error from game #26485007
Testing done on the proposed changes
I'm not sure how to reproduce this.
Checklist
- [ ] Changes are annotated, including comments where usefulSummary by CodeRabbit