2
if (nullptr!=timing_info)
{
   timing_info->h = start_time;
}

I get the following warning

autosar_cpp14 a5-1-1 violation
Using literal "NULL" other than type initialization, where symbolic names shall be used instead.

The autosar rule a5-1-1 reads

Rule A5-1-1 (required, implementation, partially automated) Literal values shall not be used apart from type initialization, otherwise symbolic names shall be used instead.

I never thought "nullptr" was a literal value. If it is a literal value, then what is the best way to handle this warning.

pasbi
  • 2,037
  • 1
  • 20
  • 32
Deepak Kiran
  • 195
  • 3
  • 15
  • I have no clue what autosar is, but does `if (timing_info)` remove the warning? It literally does the same thing, but who knows... `nullptr` is a literal value, yes. – DeiDei Jan 27 '20 at 12:38
  • when I simply use ´´´ if (timing_info) ´´´, I get warning, the if statement is only suppposed to handle boolean statements. So I can't do that as well. – Deepak Kiran Jan 27 '20 at 13:56
  • how is nullptr defined? – Snake Sanders Jan 27 '20 at 20:29
  • Would it work, if you change the condition to `if (timing_info != nullptr)`? BTW. searching through the AR document, I even found a similar statement than yours, and it was neither marked with compliant or non-compliant. – kesselhaus Jan 29 '20 at 00:13

1 Answers1

1

As you've already quoted, Rule A5-1-1 says

Rule A5-1-1 (required, implementation, partially automated)Literal values shall not be used apart from type initialization, otherwise symbolic names shall be used instead.

(source)

The idea behind this rule is that you should not use magic constants, i.e., don't write something like

// 100 is a magic constant. It's not clear what the next line means without comments.
if (list.size() > 100) {
  std::cout << "error!";
} else {
  std::cout << "everything ok!";
}

but rather, write

static constexpr auto maximum_allowed_size = 100;
// The next line speaks for itself: "if list is bigger than allowed ..."
if (list.size() > maximum_allowed_size) {
  std::cout << "error!";
} else {
  std::cout << "everything ok!";
}

This extra constant increases readability in most cases. Since nullptr is a literal and you use that literal nullptr for something else than "type initialization", your code violates that rule A5-1-1.

I don't know if autosar intentionally discourages the use of literal nullptr, I personally don't see a reason why one should do this. Maybe it has been overseen (should be an exception).

You can rephrase your code to silence the checker:

if (timing_info)  // implicitly convert pointer to bool

as that variant apparently makes the checker unhappy, too, here is another variant:

if (!!timing_info)  // convert pointer to bool using double negation

You could also use casts, but I wouldn't do that. Frankly speaking, I like the original variant (if (nullptr != timing_info)) most.

pasbi
  • 2,037
  • 1
  • 20
  • 32