Skip to content
  • Hans Johnson's avatar
    COMP: Prefer const member functions · 4c7f64eb
    Hans Johnson authored
    Finds non-static member functions that can be made const because the functions
    don’t use this in a non-const way.
    
    This check tries to annotate methods according to logical constness (not
    physical constness). Therefore, it will suggest to add a const qualifier to a
    non-const method only if this method does something that is already possible
    though the public interface on a const pointer to the object:
    
    reading a public member variable calling a public const-qualified member
    function returning const-qualified this passing const-qualified this as a
    parameter.  This check will also suggest to add a const qualifier to a
    non-const method if this method uses private data and functions in a limited
    number of ways where logical constness and physical constness coincide:
     - reading a member variable of builtin type
    Specifically, this check will not suggest to add a const to a non-const
    method if the method reads a private member variable of pointer type because
    that allows to modify the pointee which might not preserve logical constness.
    For the same reason, it does not allow to call private member functions or
    member functions on private member variables.
    
    In addition, this check ignores functions that
     - are declared virtual
     - contain a const_cast
     - are templated or part of a class template
     - have an empty body
     - do not (implicitly) use this at all (see readability-convert-member-functions-to-static).
    4c7f64eb