0

Need to stop(or minimize) usage of raw C/C++ pointers in our source base (i.e. instead other options like references or smart pointers should be used).

Is it possible to write a CLang plugin to warn on use of Raw pointers (as local/global/member variables, as function return value, as function parameter) ? Or is this a definitely impossible thing ?

If it is possible, appreciate if you could provide some guidance/references.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
aKumara
  • 395
  • 1
  • 12
  • 4
    Just a note. Don't demonize raw pointers. It still can be used when you need to use variable what can be `nullptr`, but without ownership change. See CppCoreGuidlines [1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r3-a-raw-pointer-a-t-is-non-owning) and [2](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r30-take-smart-pointers-as-parameters-only-to-explicitly-express-lifetime-semantics) – P. Dmitry Oct 10 '19 at 10:56
  • @P.Dmitry You could always use `std::optional>` instead ;-) – eerorika Oct 10 '19 at 11:11
  • @eerorika What about functions like `write(ptr, size);`? – P. Dmitry Oct 10 '19 at 11:17
  • @eerorika, although not what I asked for, your idea seems a good idea. (I currently use my own class called UnOwnedPtr for this). – aKumara Oct 11 '19 at 07:41
  • @eerorika, this(my class) is some what similar to std::experimental::observer_ptr https://en.cppreference.com/w/cpp/experimental/observer_ptr – aKumara Oct 11 '19 at 08:05
  • @aKumara I was mostly joking. Using observer_ptr or your own version it would be better. And using a bare pointer is fine too. – eerorika Oct 11 '19 at 10:50

0 Answers0