Skip to content

[clang-tidy] modernize-loop-convert: loops not detected when the iterator type has a non-trivial destructor #234990

Description

@kmate19

version: clang-tidy 22.1.6 on x64 windows (also reproduced on osx arm64 with the same version)

struct It {
  int *p;
  ~It() {}  // remove this line and the loop is flagged
  int &operator*() const { return *p; }
  It &operator++() { ++p; return *this; }
  bool operator!=(It const &o) const { return p != o.p; }
};
struct C {
  int d[4];
  It begin() { return {d}; }
  It end() { return {d + 4}; }
};
int f(C &c) {
  int s = 0;
  for (It it = c.begin(); it != c.end(); ++it) s += *it;
  return s;
}
clang-tidy --checks='-*,modernize-loop-convert' dtor.cpp -- -std=c++20

expected: use range-based for loop instead
actual: no output (also at MinConfidence: risky). Without the destructor, the warning appears.

MSVC STL with _ITERATOR_DEBUG_LEVEL=2 (default for debug builds) gives all standard iterators a nontrivial destructor so the check never fires on any iterator loop in Windows debug builds levels 0 and 1 work.

godbolt: https://godbolt.org/z/7nx565hM5

Activity

  1. llvmorg-github-actions commented on Oct 23, 2026

    @llvmorg-github-actions

    @llvm/issue-subscribers-clang-tidy

    Author: kmate19

    Details version: clang-tidy 22.1.6 on x64 windows (also reproduced on osx arm64 with the same version)
    struct It {
      int *p;
      ~It() {}  // remove this line and the loop is flagged
      int &operator*() const { return *p; }
      It &operator++() { ++p; return *this; }
      bool operator!=(It const &o) const { return p != o.p; }
    };
    struct C {
      int d[4];
      It begin() { return {d}; }
      It end() { return {d + 4}; }
    };
    int f(C &c) {
      int s = 0;
      for (It it = c.begin(); it != c.end(); ++it) s += *it;
      return s;
    }
    clang-tidy --checks='-*,modernize-loop-convert' dtor.cpp -- -std=c++20

    expected: use range-based for loop insteadactual:** no output (also at MinConfidence: risky). Without the destructor, the warning appears.

  2. yashaggarwal85d commented on Oct 27, 2026

    @yashaggarwal85d
    Contributor

    Hi I would like to take this one, almost have a PR ready

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions