Skip to content

clang-tidy: check and fix cppcoreguidelines-special-member-functions#3053

Open
Mohit242-bit wants to merge 1 commit intopgRouting:developfrom
Mohit242-bit:fix/cppcoreguidelines-special-member-functions
Open

clang-tidy: check and fix cppcoreguidelines-special-member-functions#3053
Mohit242-bit wants to merge 1 commit intopgRouting:developfrom
Mohit242-bit:fix/cppcoreguidelines-special-member-functions

Conversation

@Mohit242-bit
Copy link
Contributor

@Mohit242-bit Mohit242-bit commented Feb 5, 2026

Fixes # 3042

#summary
This PR enables and fixes the cppcoreguidelines-special-member-functions clang-tidy check.

Changes proposed in this pull request:

  • Removed the check from the disabled list in .clang-tidy
  • Fixed 6 classes across 5 header files by adding missing special member functions (destructor, copy constructor, copy assignment operator) following the Rule of Five

@pgRouting/admins

Summary by CodeRabbit

  • Refactor

    • Improved memory ownership and lifecycle handling across graph and routing components for greater stability and safety.
    • Made several algorithmic classes non-copyable to prevent inadvertent resource errors.
  • Chores

    • Adjusted static analysis configuration to relax a special-member-function check.

Copilot AI review requested due to automatic review settings February 5, 2026 00:58
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 5, 2026

Walkthrough

Multiple headers were edited to explicitly declare special member functions: several classes made non-copyable/non-movable, some gained defaulted special members, and raw pointer ownership was replaced with std::unique_ptr in Yen-related code. Changes are limited to declarations and ownership semantics.

Changes

Cohort / File(s) Summary
Chinese Postman & Points graphs
include/chinese/chinesePostman.hpp, include/withPoints/withPoints.hpp
Deleted copy/move constructors and assignment operators to enforce non-copyable/non-movable semantics for PgrDirectedChPPGraph and Pg_points_graph.
Line vertex & VRP solution
include/cpp_common/line_vertex.hpp, include/vrp/solution.hpp
Added defaulted special members (copy/move assignment, move ctor, destructor) for Line_vertex; added ~Solution() = default; to pgrouting::vrp::Solution.
Yen KSP visitor ownership
include/yen/ksp.hpp, include/yen/turnRestrictedPath.hpp
Replaced raw Visitor* with std::unique_ptr<Visitor>, initialize with std::make_unique, defaulted destructors, and deleted copy/move operations for Pgr_ksp and nested Visitor; updated turnRestrictedPath to use std::make_unique.
Config
.clang-tidy
Removed cppcoreguidelines-special-member-functions from the clang-tidy checks.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

Enhancement, C/C++

Suggested reviewers

  • cvvergara
  • robe2

Poem

🐇 I nibble headers, tidy each rule,
Deleted copies — no more accidental school.
Smart pointers cradle visitors tight,
Destructors default, everything’s right.
Hops of joy for safe ownership tonight!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 9.09% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main objective: enabling the clang-tidy check for special member functions and fixing violations across the codebase.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
include/cpp_common/line_vertex.hpp (1)

58-63: 🧹 Nitpick | 🔵 Trivial

Consider simplifying the copy constructor.

The user-defined copy constructor performs the same member-by-member copy that the compiler-generated default would provide. Consider defaulting it for consistency with the newly added defaulted assignment operator:

Line_vertex(const Line_vertex&) = default;

This would also make the cp_members method potentially redundant.

include/chinese/chinesePostman.hpp (1)

107-109: 🧹 Nitpick | 🔵 Trivial

Destructor only clears one container; others auto-destruct.

The destructor explicitly clears edgeToIdx while other member containers (originalEdges, resultEdges, resultGraph, etc.) are left to their automatic destruction. If explicit clearing is intended for a specific reason (e.g., deterministic order), consider documenting it. Otherwise, this manual clear is unnecessary since the map's destructor handles cleanup.

Consider simplifying the destructor
 PgrDirectedChPPGraph::~PgrDirectedChPPGraph() {
-    edgeToIdx.clear();
 }

Or if the destructor body becomes empty, you could default it in the declaration and remove the definition entirely.

🤖 Fix all issues with AI agents
In `@include/yen/ksp.hpp`:
- Line 66: Replace raw pointer ownership of m_vis with std::unique_ptr to avoid
manual new/delete: change the member declaration type of m_vis to
std::unique_ptr<Visitor>, initialize it with std::make_unique<Visitor>() (e.g.,
in the constructor initializer where currently m_vis(new Visitor) is used),
remove any manual delete or custom destructor and default the destructor, and
ensure all existing usages of m_vis-> remain unchanged; also apply the same
replacement for the other m_vis occurrence referenced in the diff.

Copy link

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 PR enables the cppcoreguidelines-special-member-functions clang-tidy check and fixes violations across 6 classes by adding missing special member functions according to the Rule of Five/Rule of Zero principle.

Changes:

  • Removed cppcoreguidelines-special-member-functions from the disabled checks list in .clang-tidy
  • Added missing special member functions (copy constructors, copy assignment operators, destructors) to 6 classes across 5 header files
  • Ensured non-copyable classes have deleted copy operations and copyable classes have appropriate implementations

Reviewed changes

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

Show a summary per file
File Description
.clang-tidy Removed cppcoreguidelines-special-member-functions from the disabled checks list
include/yen/ksp.hpp Added deleted copy constructor and copy assignment operator to Pgr_ksp class; added default constructor and deleted copy operations to Visitor class
include/withPoints/withPoints.hpp Added deleted copy assignment operator and default destructor to Pg_points_graph class
include/vrp/solution.hpp Added default destructor to Solution class
include/cpp_common/line_vertex.hpp Added default copy assignment operator and default destructor to Line_vertex class
include/chinese/chinesePostman.hpp Added deleted copy constructor and copy assignment operator to PgrDirectedChPPGraph class

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

@cvvergara cvvergara marked this pull request as draft February 5, 2026 17:31
@cvvergara cvvergara marked this pull request as draft February 5, 2026 17:31
@Mohit242-bit Mohit242-bit force-pushed the fix/cppcoreguidelines-special-member-functions branch 5 times, most recently from 28ec4d6 to 7555f35 Compare February 8, 2026 10:25
@Mohit242-bit Mohit242-bit marked this pull request as ready for review February 8, 2026 10:26
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
include/vrp/solution.hpp (1)

68-83: ⚠️ Potential issue | 🟠 Major

Rule of Five incomplete — move constructor and move assignment operator are missing.

The class declares a copy constructor, copy assignment operator, and destructor, but no move constructor or move assignment operator. Under the strict Rule of Five (which cppcoreguidelines-special-member-functions enforces by default), all five should be declared. The compiler will implicitly delete the move operations here since a copy constructor is user-declared, but the clang-tidy check may still flag this unless AllowMissingMoveFunctions is enabled.

Other classes in this PR (e.g., Pg_points_graph, PgrDirectedChPPGraph) explicitly declare all five. Consider adding:

Proposed fix
      /* `@brief` copy assignment */
      Solution& operator = (const Solution& sol) {
          EPSILON = 0.0001;
          fleet = sol.fleet;
          trucks = sol.trucks;
          return *this;
      }

+     /* `@brief` move constructor */
+     Solution(Solution&&) = default;
+
+     /* `@brief` move assignment */
+     Solution& operator=(Solution&&) = default;
+
      /* `@brief` destructor */
      ~Solution() = default;
#!/bin/bash
# Check if AllowMissingMoveFunctions is configured in .clang-tidy
fd '.clang-tidy' --max-depth 2 --exec cat {}
include/cpp_common/line_vertex.hpp (1)

58-68: 🧹 Nitpick | 🔵 Trivial

Consider defaulting the copy constructor as well.

The user-defined copy constructor (lines 58–63) manually copies every member, which is identical to what = default would generate. Since the new copy assignment, move operations, and destructor are all defaulted, the copy constructor could follow suit for consistency and reduced maintenance burden.

Proposed simplification
-     Line_vertex(const Line_vertex &v) :
-         id(v.id),
-         vertex_id(v.vertex_id),
-         source(v.source),
-         target(v.target),
-         cost(v.cost) {}
+     Line_vertex(const Line_vertex&) = default;

@Mohit242-bit
Copy link
Contributor Author

Hi @cvvergara ,

This PR addresses the remaining cppcoreguidelines-special-member-functions violations (#3034).

Summary of Changes:

  • Refactored Pgr_ksp to use std::unique_ptr for modern memory management.
  • Implemented the Rule of Five:
    • Defaulted operations for POD types .
    • Deleted copy/move operations for non-copyable classes Pg_points_graph, PgrDirectedChPPGraph , Visitor to prevent object slicing and resource duplication.
    • Added destructor to Solution.

Verified with local build . Would appreciate a review when you get a chance.

@Mohit242-bit Mohit242-bit marked this pull request as draft February 8, 2026 12:08
- Modernize Pgr_ksp to use std::unique_ptr instead of raw pointer
- Complete Rule of Five for Pgr_ksp, Visitor, Pg_points_graph,
  PgrDirectedChPPGraph, Solution, and Line_vertex classes
- Update turnRestrictedPath to use std::make_unique
@Mohit242-bit Mohit242-bit force-pushed the fix/cppcoreguidelines-special-member-functions branch from 7555f35 to 6b7268d Compare February 8, 2026 12:20
@Mohit242-bit Mohit242-bit marked this pull request as ready for review February 8, 2026 12:49
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
include/vrp/solution.hpp (2)

70-83: ⚠️ Potential issue | 🟠 Major

Rule of Five is incomplete — move constructor and move assignment are missing.

The class now declares a copy constructor, copy assignment operator, and destructor, but no move constructor or move assignment operator. The cppcoreguidelines-special-member-functions check requires all five to be declared when any are user-provided. This will likely still trigger the clang-tidy warning this PR aims to fix.

Either default or delete them:

Proposed fix
      /* `@brief` copy assignment */
      Solution& operator = (const Solution& sol) {
          EPSILON = 0.0001;
          fleet = sol.fleet;
          trucks = sol.trucks;
          return *this;
      }

+     /* `@brief` move constructor */
+     Solution(Solution&&) = default;
+
+     /* `@brief` move assignment */
+     Solution& operator=(Solution&&) = default;
+
      /* `@brief` destructor */
      ~Solution() = default;

68-69: 🧹 Nitpick | 🔵 Trivial

Copy constructor and copy assignment hardcode EPSILON instead of copying from source.

Both the copy constructor (line 69: EPSILON(0.0001)) and the copy assignment (line 76: EPSILON = 0.0001;) ignore sol.EPSILON. If EPSILON is ever changed on an instance, the copy will silently reset it. This is pre-existing behavior, but worth flagging since the area is being touched.

Proposed fix
      Solution(const Solution &sol) :
-         EPSILON(0.0001),
+         EPSILON(sol.EPSILON),
          fleet(sol.fleet),
          trucks(sol.trucks)
     {};

      Solution& operator = (const Solution& sol) {
-         EPSILON = 0.0001;
+         EPSILON = sol.EPSILON;
          fleet = sol.fleet;
          trucks = sol.trucks;
          return *this;
      }

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.

1 participant