c++ 如何防止短的重复被分成两行?

nxagd54h  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(152)

我有一个关于C++编译器的clang格式问题。我注意到在升级到Clang-Format 15.0.1后,简短但不在线的编译器有了新的行为。我在下面称之为“两行编译器”。示例:

  • 一行双床间**
  1. // good/unchanged
  2. auto shortLambda= []() { doSomething(); };

字符串

两行字符串:

  1. // unwanted/new behavior
  2. const auto mediumLambda = [some, args, here](More arguments)
  3. { return doSomething(some, args, here, arguments); };

多行Lambdas:

  1. // good/unchanged
  2. const auto longLambda= [some, args, here](Are here)
  3. {
  4. auto result =doSomething(some, args, here, arguments);
  5. return result;
  6. };

两行队列的期望行为:

  1. // desired
  2. const auto mediumLambda = [some, args, here](More arguments)
  3. {
  4. return doSomething(some, args, here, arguments);
  5. };

理性:“两行”,如果语句没有在两行上挤在一起

  1. // Good/unchanged
  2. if (condition)
  3. {
  4. doWhatever();
  5. }
  6. // Bad/unconfigured:
  7. if (condition)
  8. { doWhatever(); }


我可以通过设置AllowShortLambdasOnASingleLine : None使所有的多行文件都是多行的--但是我确实希望那些可以放在一行上的文件可以放在一行上。

我如何避免“两行重叠”?

相关配置:

  1. # Basic
  2. Language : Cpp
  3. Standard : c++20
  4. ColumnLimit : '110'
  5. # Tabs
  6. TabWidth : '3'
  7. IndentWidth : '3'
  8. UseTab : Never
  9. # Braces/Parens (Allman style)
  10. BreakBeforeBraces : Custom
  11. BraceWrapping:
  12. AfterCaseLabel: true
  13. AfterClass: true
  14. AfterControlStatement: true
  15. AfterEnum: true
  16. AfterFunction: true
  17. AfterNamespace: true
  18. AfterObjCDeclaration: true
  19. AfterStruct: true
  20. AfterUnion: true
  21. AfterExternBlock: true
  22. BeforeCatch: true
  23. BeforeElse: true
  24. BeforeLambdaBody: true
  25. IndentBraces: false
  26. SplitEmptyFunction: true
  27. SplitEmptyRecord: true
  28. SplitEmptyNamespace: true
  29. Cpp11BracedListStyle : true
  30. SpaceBeforeParens : ControlStatements
  31. SpacesInParentheses : false
  32. SpaceInEmptyParentheses : false
  33. # Functions
  34. AlignAfterOpenBracket : Align
  35. AllowShortBlocksOnASingleLine : false
  36. AllowShortFunctionsOnASingleLine : Inline
  37. BinPackArguments : true
  38. BinPackParameters : true
  39. AllowAllParametersOfDeclarationOnNextLine : true
  40. IndentWrappedFunctionNames : false
  41. AlwaysBreakAfterReturnType : None
  42. AlwaysBreakAfterDefinitionReturnType : None
  43. # Lambdas
  44. AllowShortLambdasOnASingleLine : All


编辑:添加了一些使C++有效的函数。

5ssjco0h

5ssjco0h1#

恐怕这是不可能的。如果让我猜的话,该工具首先检查您是否允许短的字符串,然后注意到它不适合在一行中,所以它应用BraceWrapping: BeforeLambdaBody并满足,因为现在所有内容都在列限制之内。
如果在这种情况下可以牺牲Allman的风格,则将BraceWrapping: BeforeLambdaBody设置为false将导致

  1. const auto mediumLambda = [some, args, here](More arguments) {
  2. return doSomething(some, args, here, arguments);
  3. };

字符串
同时在一行中保持短的双引号,如果不可能保持在一行中,则断开结尾};
它可能是一个bug,也可能不是,但它肯定值得填写一个bug票据来改变这种行为或改进文档,解释该工具在这种特定情况下的作用。

相关问题