Skip to content

[docs][clang-tidy] Correct StrictMode example in modernize-use-std-print #108805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 17, 2024

Conversation

MainakSil
Copy link
Contributor

Updated the example in the StrictMode section of the clang-tidy check modernize-use-std-print.

The previous example incorrectly swapped the cast of signed and unsigned integers. Specifically:

  • The signed integer i was being cast to unsigned int, and
  • The unsigned integer u was being cast to int.

This correction ensures that the behavior of std::print with StrictMode enabled matches that of printf, by reversing the casts to maintain the correct signedness.

Issue Refference
It solves #101397

Updated the example in the StrictMode section of the clang-tidy check modernize-use-std-print.

The previous example incorrectly swapped the cast of signed and unsigned integers. Specifically:

The signed integer i was being cast to unsigned int, and
The unsigned integer u was being cast to int.
This correction ensures that the behavior of std::print with StrictMode enabled matches that of printf, by reversing the casts to maintain the correct signedness.

Issue Refference
It solves #llvm#101397
@llvmbot
Copy link
Member

llvmbot commented Sep 16, 2024

@llvm/pr-subscribers-clang-tidy

Author: Mainak Sil (MainakSil)

Changes

Updated the example in the StrictMode section of the clang-tidy check modernize-use-std-print.

The previous example incorrectly swapped the cast of signed and unsigned integers. Specifically:

  • The signed integer i was being cast to unsigned int, and
  • The unsigned integer u was being cast to int.

This correction ensures that the behavior of std::print with StrictMode enabled matches that of printf, by reversing the casts to maintain the correct signedness.

Issue Refference
It solves #101397


Full diff: https://github.com/llvm/llvm-project/pull/108805.diff

1 Files Affected:

  • (modified) clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst (+1-1)
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst
index 59bb722e2c24fc..a825cd7432a57d 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst
@@ -109,7 +109,7 @@ Options
 
   .. code-block:: c++
 
-    std::print("{} {}\n", static_cast<unsigned int>(i), static_cast<int>(u));
+    std::print("{} {}\n", static_cast<int>(u), static_cast<unsigned int>(i));
 
   to ensure that the output will continue to be the unsigned representation
   of `-42` and the signed representation of `0xffffffff` (often

@llvmbot
Copy link
Member

llvmbot commented Sep 16, 2024

@llvm/pr-subscribers-clang-tools-extra

Author: Mainak Sil (MainakSil)

Changes

Updated the example in the StrictMode section of the clang-tidy check modernize-use-std-print.

The previous example incorrectly swapped the cast of signed and unsigned integers. Specifically:

  • The signed integer i was being cast to unsigned int, and
  • The unsigned integer u was being cast to int.

This correction ensures that the behavior of std::print with StrictMode enabled matches that of printf, by reversing the casts to maintain the correct signedness.

Issue Refference
It solves #101397


Full diff: https://github.com/llvm/llvm-project/pull/108805.diff

1 Files Affected:

  • (modified) clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst (+1-1)
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst
index 59bb722e2c24fc..a825cd7432a57d 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-print.rst
@@ -109,7 +109,7 @@ Options
 
   .. code-block:: c++
 
-    std::print("{} {}\n", static_cast<unsigned int>(i), static_cast<int>(u));
+    std::print("{} {}\n", static_cast<int>(u), static_cast<unsigned int>(i));
 
   to ensure that the output will continue to be the unsigned representation
   of `-42` and the signed representation of `0xffffffff` (often

Copy link
Contributor

@nicovank nicovank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the example should be:

printf("%u %d\n", i, u); // Change here.
// transforms to:
std::println("{} {}", static_cast<unsigned int>(i), static_cast<int>(u)); // No change here.

@@ -109,7 +109,7 @@ Options

.. code-block:: c++

std::print("{} {}\n", static_cast<unsigned int>(i), static_cast<int>(u));
std::print("{} {}\n", static_cast<int>(u), static_cast<unsigned int>(i));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The arguments shouldn't be swapped, I think the initial code block above must swap the %d and %u specifier for this example to make sense.

@nicovank
Copy link
Contributor

cc @mikecrowe

@MainakSil
Copy link
Contributor Author

Please check now.

@mikecrowe
Copy link
Contributor

Thanks for fixing this @MainakSil. I wasn't aware of the bug report until now.

@nicovank is correct that only the format string needs to change as I got the format specifiers the wrong way round originally. Thanks for adding me to this change.

I copied the same mistake to use-std-format.rst if you'd like to fix that one too in this change. If not then I will do so.

Thanks.

@MainakSil
Copy link
Contributor Author

Thanks for pointing that out, @mikecrowe! I've gone ahead and fixed the same mistake in use-std-format.rst as well in this PR.

Copy link
Contributor

@mikecrowe mikecrowe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for fixing this.

@MainakSil
Copy link
Contributor Author

Please merge it if it looks good to you guys...

@nicovank nicovank merged commit 2bda9e1 into llvm:main Sep 17, 2024
9 checks passed
@MainakSil MainakSil deleted the MainakSil-patch-2 branch September 17, 2024 17:04
hamphet pushed a commit to hamphet/llvm-project that referenced this pull request Sep 18, 2024
tmsri pushed a commit to tmsri/llvm-project that referenced this pull request Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants