Most active commenters
  • amluto(3)

←back to thread

327 points AareyBaba | 11 comments | | HN request time: 0.001s | source | bottom
Show context
time4tea ◴[] No.46184345[source]
a = a; // misra

Actual code i have seen with my own eyes. (Not in F-35 code)

Its a way to avoid removing an unused parameter from a method. Unused parameters are disallowed, but this is fine?

I am sceptical that these coding standards make for good code!

replies(11): >>46184442 #>>46184460 #>>46184571 #>>46185232 #>>46185373 #>>46186276 #>>46186377 #>>46186457 #>>46186510 #>>46186705 #>>46189488 #
unwind ◴[] No.46184460[source]
For C, the proper/expected/standard way to reference a variable without accessing it is a cast to void:

    (void) a;
I'm sure there are commonly-implemented compiler extensions, but this is the normal/native way and should always work.
replies(1): >>46185753 #
1. amluto ◴[] No.46185753[source]
Not if you use GCC.

https://godbolt.org/z/zYdc9ej88

clang gets this right.

replies(4): >>46185785 #>>46185906 #>>46191931 #>>46203376 #
2. comex ◴[] No.46185785[source]
It does work in GCC to suppress unused variable warnings. Just not for function calls I guess.
replies(1): >>46189702 #
3. Am4TIfIsER0ppos ◴[] No.46185906[source]
You've defined that function with an attribute saying not to ignore the returned value. Is it right to explicitly silence an explicit warning?
replies(2): >>46186059 #>>46187394 #
4. MathMonkeyMan ◴[] No.46186059[source]
Sometimes. For example, you might be setting a non-crucial option on a socket, and if it fails you don't even care to log the fact (maybe the logging would be too expensive), so you just ignore the return value of whatever library is wrapping setsockopt.
5. amluto ◴[] No.46187394[source]
I want some defined way to tell the compiler that I am intentionally ignoring the result.

I encounter this when trying to do best-effort logging in a failure path. I call some function to log and error and maybe it fails. If it does, what, exactly, am I going to do about it? Log harder?

replies(1): >>46189209 #
6. dotancohen ◴[] No.46189209{3}[source]
Yes.

When my database logging fails, I write a file that logs the database fail (but not the original log file).

When my file logging fails, depending on application, I'll try another way of getting the information (the fact that for file logging failed) out - be that an http request or an email or something else.

Databases fail, file systems fill up. Logging logging failures is extremely important.

replies(1): >>46189383 #
7. amluto ◴[] No.46189383{4}[source]
And when that last way fails, what do you do?

I like to have a separate monitoring process that monitors my process and a separate machine in a different datacenter monitoring that. But at the end of the day, the first process is still going to do try to log, detect that it failed, try the final backup log and then signal to its monitor that it’s in a bad state. It won’t make any decisions that depend on whether the final backup logging succeeds or fails.

replies(1): >>46190349 #
8. cminmin ◴[] No.46189702[source]
__attribute__((maybe_unused)) or [[maybe_unused]] or such things (dependin on ur spec version i guess?) can be used not to disable a whole line of errors.
9. dotancohen ◴[] No.46190349{5}[source]
I'm not working on anything life-critical, one additional layer of redundancy is all I budget for. If both my database is down and my local filesystem is full simultaneously, things have gone bad and I've likely got lots of other symptoms I'm going to find to direct me.
10. gpderetta ◴[] No.46191931[source]
interestingly it works for [[nodiscard]]!

and assigning to std::ignore works for both.

11. account42 ◴[] No.46203376[source]
a) __attribute__((warn_unused_result)) is non-standard in the first place, are you looking for [[nodiscard]] - GCC does not warn on cast to void with that?

b) A return value that is explicitly marked like this is very different from an unused variable that gp suggested the cast to void idiom for. GCC does not warn on variables that are unused except for a cast to void.