Skip to content

Resolving a few CodeQL warnings#2115

Merged
HammerGS merged 2 commits intomainfrom
issues
Jan 17, 2026
Merged

Resolving a few CodeQL warnings#2115
HammerGS merged 2 commits intomainfrom
issues

Conversation

@SJuliez
Copy link
Member

@SJuliez SJuliez commented Jan 17, 2026

No description provided.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request addresses CodeQL warnings by improving null safety checks and code formatting. The changes remove unnecessary null checks where variables cannot be null, add defensive null checks where methods could be called during super constructor execution, and improve code readability with proper brace formatting.

Changes:

  • Added explanatory comments for null checks required during super constructor execution in setFont() methods
  • Removed unnecessary null checks in initialize() methods where fields are guaranteed to be non-null
  • Added defensive null checks in getPreferredSize() methods with fallback to super implementation
  • Removed impossible null checks for UnitMemento constructor result and improved code formatting with consistent brace usage

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
SummaryWeightLabel.java Updated null safety checks for internalSizingLabel with clarifying comments and defensive programming in getPreferredSize()
SummaryAvailabilityLabel.java Applied identical null safety improvements as SummaryWeightLabel for consistency
MegaMekLabMainUI.java Removed unnecessary null check for constructor result and improved code formatting with explicit braces

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 73 to 77
if (internalSizingLabel != null) { // KEEP THIS - could be called before initialization
return internalSizingLabel.getPreferredSize();
} else {
return super.getPreferredSize();
}
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

The comment states "could be called before initialization" but the null check logic seems inconsistent with the removal of the null check in the initialize() method. If getPreferredSize() can be called before initialization (before the constructor completes and internalSizingLabel is set), then initialize() should also have a null check since it's called from the constructor. However, since internalSizingLabel is a final field initialized at declaration (line 47), it will always be non-null after the field initialization phase. The null check in getPreferredSize() may be addressing a scenario where the method is called during super constructor execution (similar to setFont), but the comment should clarify this is only for super constructor calls, not general "before initialization".

Suggested change
if (internalSizingLabel != null) { // KEEP THIS - could be called before initialization
return internalSizingLabel.getPreferredSize();
} else {
return super.getPreferredSize();
}
return internalSizingLabel.getPreferredSize();

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

see below

@Override
public Dimension getPreferredSize() {
return internalSizingLabel.getPreferredSize();
if (internalSizingLabel != null) { // KEEP THIS - could be called before initialization
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

The comment states "could be called before initialization" but the null check logic seems inconsistent with the removal of the null check in the initialize() method. If getPreferredSize() can be called before initialization (before the constructor completes and internalSizingLabel is set), then initialize() should also have a null check since it's called from the constructor. However, since internalSizingLabel is a final field initialized at declaration (line 43), it will always be non-null after the field initialization phase. The null check in getPreferredSize() may be addressing a scenario where the method is called during super constructor execution (similar to setFont), but the comment should clarify this is only for super constructor calls, not general "before initialization".

Suggested change
if (internalSizingLabel != null) { // KEEP THIS - could be called before initialization
if (internalSizingLabel != null) { // KEEP THIS - defensive in case called from the super constructor before field initialization

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

good suggestion

public void setFont(Font font) {
super.setFont(font);
if (internalSizingLabel != null) {
if (internalSizingLabel != null) { // KEEP THIS - called from the super constructor!
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

This check is useless. internalSizingLabel cannot be null at this check, since new JLabel(...) always is non-null.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

setFont is called from the super constructor. Easily seen when removing the check

public void setFont(Font font) {
super.setFont(font);
if (internalSizingLabel != null) {
if (internalSizingLabel != null) { // KEEP THIS - called from the super constructor!
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

This check is useless. internalSizingLabel cannot be null at this check, since new JLabel(...) always is non-null.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

setFont is called from the super constructor. Easily seen when removing the check

Copy link
Collaborator

@Sleet01 Sleet01 left a comment

Choose a reason for hiding this comment

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

LGTM

@HammerGS HammerGS merged commit 3fe5647 into main Jan 17, 2026
6 checks passed
@HammerGS HammerGS deleted the issues branch January 17, 2026 20:21
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