0

I use a struct to transfer data over TCP-IP and I have to stick with certain packet size, so I use char array of fixed size for text data. Due to the fact that I can't initialize it otherwise, I forced to copy string to that array in constructor using simple function (based on strcpy). The problem is: analyzer (clang-tidy Ubuntu LLVM 14.0.0) tells me

warning: constructor does not initialize these fields: receiver [cppcoreguidelines-pro-type-member-init]

but in fact that is not true. Tell me, please, how do I change my code to suppress those warnings messages? I understand, that I can initialize those arrays with zeros using {} and than fill them with the needed data, but it looks like double work..

inline void copyName(char *dst, const char *src) {
    strncpy(dst, src, MAX_NAME_LENGTH);
}

struct Header {
    const MessageType type;
    char receiver[MAX_NAME_LENGTH];
    static uint32_t messageId;
    Header();
    Header(MessageType type, const char* receiver);
};

Header::Header(const MessageType type, const char *receiverName)
    : type(type) // the warning is here
{
    copyName(receiver, receiverName);
    Header::messageId++;
}

P.S> Found some workaround (which, of course, not the answer to the actual question): there is the option IgnoreArrays for that warning which, when set to true, suppresses those warnings

  • Can you also include the code for `copyName` please ? – Richard Critten Sep 10 '22 at 13:26
  • 1
    [Boost.Asio](https://www.boost.org/doc/libs/release/doc/html/boost_asio/overview.html) provides char-array-free, portable networking functionality. – DevSolar Sep 10 '22 at 14:09
  • @DevSolar, thanks, but I'm really limited in abilities on the target platform. I can only use bare c++ –  Sep 10 '22 at 15:06
  • Please post the full error message and tell us what line it happens on. Also mention which version of clang-tidy. – John Zwinck Sep 10 '22 at 15:17
  • @JohnZwinck done –  Sep 10 '22 at 15:29
  • @ValeriyChistyakov: As in...? You are *obviously* allowed to use library functions, because "bare C++" doesn't offer networking (yet). Is this a homework assignment with artificial limitations? What *is* your target platform? The way you are doing things here is very C-ish, with all the problems that brings. – DevSolar Sep 10 '22 at 17:02
  • @DevSolar platform can be any (including 8bit microcontroller). By the company standards only QT is used as third part on our side (which where network come from). And yes, I know about `QDataStream` but on the other side can be our client's machine which don't know how to deal with the data which it will generate. That's why I use C-ish style. Can you provide another structure of data which would't be so old-school and could be sent through network? –  Sep 11 '22 at 05:32
  • TCP is TCP is TCP. The library / class employed should not have any impact on the data sent. I don't know about `QDataStream` but would be very surprised if it were any different in this regard. (Consider that a network library that didn't "talk standard" would be unable to interface with HTTP / SSH / ... just as well as your protocol.) – DevSolar Sep 11 '22 at 10:33

1 Answers1

1

The warning is a false positive. The clang-tidy docs for the warning you got say:

The check takes assignment of fields in the constructor body into account but generates false positives for fields initialized in methods invoked in the constructor body.

https://releases.llvm.org/10.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/cppcoreguidelines-pro-type-member-init.html

Seems like a poor check to me, but if you want to keep it enabled you might be able to suppress this one instance of it using the magic // NOLINT comment...somewhere?

John Zwinck
  • 239,568
  • 38
  • 324
  • 436