|
1 |
| -use clippy_utils::diagnostics::span_lint_and_help; |
| 1 | +use clippy_utils::{diagnostics::{span_lint_and_help, span_lint_and_then, span_lint_and_sugg}, source::{indent_of, snippet}}; |
2 | 2 | use rustc_ast::Attribute;
|
| 3 | +use rustc_errors::Applicability; |
3 | 4 | use rustc_hir::{Item, ItemKind};
|
4 | 5 | use rustc_lint::{LateContext, LateLintPass};
|
5 | 6 | use rustc_middle::dep_graph::DepContext;
|
@@ -50,14 +51,34 @@ impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutRepr {
|
50 | 51 | };
|
51 | 52 |
|
52 | 53 | if !has_repr_attr(cx, attrs) {
|
53 |
| - span_lint_and_help( |
54 |
| - cx, |
55 |
| - TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR, |
56 |
| - lint_span, |
57 |
| - "trailing zero-sized array in a struct which is not marked with a `repr` attribute", |
58 |
| - None, |
59 |
| - "consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute", |
60 |
| - ); |
| 54 | + let suggestion_span = item.span.shrink_to_lo(); |
| 55 | + let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0)); |
| 56 | + |
| 57 | + span_lint_and_sugg(cx, TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR, item.span, "trailing zero-sized array in a struct which is not marked with a `repr` attribute", "consider adding `#[repr(C)]` or another `repr` attribute", format!("#[repr(C)]\n{}", snippet(cx, item.span.shrink_to_lo().to(item.ident.span), "..")), Applicability::MaybeIncorrect); |
| 58 | + |
| 59 | + // span_lint_and_then( |
| 60 | + // cx, |
| 61 | + // TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR, |
| 62 | + // item.span, |
| 63 | + // "trailing zero-sized array in a struct which is not marked with a `repr` attribute", |
| 64 | + // |diag| { |
| 65 | + // let sugg = format!("#[repr(C)]\n{}", indent); |
| 66 | + // let sugg2 = format!("#[repr(C)]\n{}", item.ident.span); |
| 67 | + // diag.span_suggestion(item.span, |
| 68 | + // "consider adding `#[repr(C)]` or another `repr` attribute", |
| 69 | + // sugg2, |
| 70 | + // Applicability::MaybeIncorrect); |
| 71 | + // } |
| 72 | + // ); |
| 73 | + |
| 74 | + // span_lint_and_help( |
| 75 | + // cx, |
| 76 | + // TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR, |
| 77 | + // lint_span, |
| 78 | + // "trailing zero-sized array in a struct which is not marked with a `repr` attribute", |
| 79 | + // None, |
| 80 | + // "consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute", |
| 81 | + // ); |
61 | 82 | }
|
62 | 83 | }
|
63 | 84 | }
|
@@ -91,7 +112,8 @@ fn has_repr_attr(cx: &LateContext<'tcx>, attrs: &[Attribute]) -> bool {
|
91 | 112 | // NOTE: there's at least four other ways to do this but I liked this one the best. (All five agreed
|
92 | 113 | // on all testcases (when i wrote this comment. I added a few since then).) Happy to use another;
|
93 | 114 | // they're in the commit history if you want to look (or I can go find them).
|
| 115 | + let sess = cx.tcx.sess(); // are captured values in closures evaluated once or every time? |
94 | 116 | attrs
|
95 | 117 | .iter()
|
96 |
| - .any(|attr| !rustc_attr::find_repr_attrs(cx.tcx.sess(), attr).is_empty()) |
| 118 | + .any(|attr| !rustc_attr::find_repr_attrs(sess, attr).is_empty()) |
97 | 119 | }
|
0 commit comments