Skip to content

Conversation

@hysw
Copy link
Collaborator

@hysw hysw commented Nov 26, 2025

Fix two memory leak, and many lint warning.

  • Created a tiny Guidelines Support Library
  • Add some helper function to make sure we do qt ownership correctly.

wangra-google
wangra-google previously approved these changes Nov 27, 2025
Copy link
Collaborator

@angela28chen angela28chen left a comment

Choose a reason for hiding this comment

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

Thanks for making this effort to make Dive ui code use "new" in a more intentioned way


// Forward declarations for UI.

class OverlayHelper;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this seems repeated (line 74)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Eh, I probably selected the wrong range when running sort+deduplicate in the editor.

return ptr; // NOLINT
}

// Create a new Qt object.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like the examples section for how to create new layouts using LayoutHelper. Can we have a similar brief example section for QtNew and QtNewUnowned?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They are just new with static_assert.
Will add a bit more document.

#include "ui/overlay.h"
#include "ui/settings.h"

using DiveLint::QtNew;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any downside to avoiding "using" and just calling the full DiveLint::QtNew() when using this or is it just wordy? I slightly prefer the wordy version since I think it makes it clearer that QtNew() isn't something defined by Qt despite the prefix

Copy link
Collaborator

Choose a reason for hiding this comment

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

Though I guess it might be confusing to see the word "Lint" in there. Could it not just be Dive::QtNew() or DiveQt::QtNew or something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Those are annotations, and more specifically lint annotations. They don't do anything other than doing additional lint checks.

Also regarding to using alias https://google.github.io/styleguide/cppguide.html#Aliases

@hysw
Copy link
Collaborator Author

hysw commented Nov 28, 2025

Thanks for making this effort to make Dive ui code use "new" in a more intentioned way

Had a bit offline chat earlier with @footballhead , instead of disable lint check for pointer ownership, it seems sensible to explicitly annotate allocations.

namespace gsl
{

template<class T, std::enable_if_t<std::is_pointer_v<T>, bool> = true> //
Copy link
Collaborator

Choose a reason for hiding this comment

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

why the trailing //?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We probably wan to remove clang-format rule AlwaysBreakTemplateDeclarations: MultiLine

- "-cppcoreguidelines-avoid-non-const-global-variables" # ABSL_FLAG
- "-cppcoreguidelines-use-enum-class"
- "-readability-redundant-access-specifiers" # qt
- "-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access" # https://abseil.io/tips/224
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 love adding the justification in the comment

Comment on lines +53 to +56
using DiveLint::QtNew;
using DiveLint::QtNewUnowned;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit, likely not an issue here since the using isn't inside the namespace but (in general) prefer to fully qualify https://abseil.io/tips/119

Suggested change
using DiveLint::QtNew;
using DiveLint::QtNewUnowned;
using ::DiveLint::QtNew;
using ::DiveLint::QtNewUnowned;

(https://abseil.io/tips/119 also recommends putting this in a namespace but I can see that there isn't one here :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

m_available_metrics(available_metrics)
{
qDebug() << "AnalyzeDialog created.";
InitializeLayout();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it worth documenting somewhere that this is called from the constructor so should not be made virtual?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It should be a factory function

auto Create() {
  auto dialog = new AnalyzeDialog;
  if (!AnalyzeDialog->InitializeLayout()) { delete dialog; return nullptr; }
  return dialog;
}

however the rest of the code are not capable of handling creation failure at the moment.

Copy link
Collaborator

@footballhead footballhead Dec 1, 2025

Choose a reason for hiding this comment

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

Until that happens, document that this is called from the constructor so should not be made virtual?


// Left Panel Layout
m_left_panel_layout = new QVBoxLayout();
m_left_panel_layout = QtNewUnowned<QVBoxLayout>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we look at using the layout helper here as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. Although that's a lot of reordering probably fitted for the next PR.

I intentionally did not add AddWidget and AddLayout since they encourage having unparented elements.

@hysw hysw force-pushed the lint-1126 branch 2 times, most recently from a3afd32 to a73af62 Compare December 1, 2025 18:10
// auto* label = layout.New<QLabel>(tr("text"));
//
// Add a new item to an existing layout:
// auto* label = NewWidgetLayout{layout}.New<QLabel>(tr("text"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: since NewWidgetLayout is a funtion call, should the {} be () instead?

Suggested change
// auto* label = NewWidgetLayout{layout}.New<QLabel>(tr("text"));
// auto* label = NewWidgetLayout(layout).New<QLabel>(tr("text"));

m_available_metrics(available_metrics)
{
qDebug() << "AnalyzeDialog created.";
InitializeLayout();
Copy link
Collaborator

@footballhead footballhead Dec 1, 2025

Choose a reason for hiding this comment

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

Until that happens, document that this is called from the constructor so should not be made virtual?

wangra-google
wangra-google previously approved these changes Dec 1, 2025
@hysw hysw force-pushed the lint-1126 branch 3 times, most recently from 6b4fffe to c4ca939 Compare December 26, 2025 00:27
@hysw hysw marked this pull request as draft December 26, 2025 00:28
@hysw hysw force-pushed the lint-1126 branch 2 times, most recently from 4920263 to ffcb8a7 Compare December 26, 2025 00:48
- Created a tiny Guidelines Support Library
- Add some helper function to make sure we do qt ownership correctly.
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.

4 participants