Skip to content

Commit

Permalink
Account for API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Oct 20, 2023
1 parent f2d06cc commit 4c5ee0c
Show file tree
Hide file tree
Showing 45 changed files with 97 additions and 62 deletions.
59 changes: 32 additions & 27 deletions examples/general/await_holding_span_guard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

extern crate rustc_hir;
extern crate rustc_middle;
extern crate rustc_span;

use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::match_def_path;
use rustc_hir::def_id::DefId;
use rustc_hir::{AsyncGeneratorKind, Body, BodyId, GeneratorKind};
use rustc_hir::{AsyncGeneratorKind, Body, GeneratorKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{Adt, GeneratorInteriorTypeCause};
use rustc_span::Span;
use rustc_middle::mir::GeneratorLayout;
use rustc_middle::ty::Adt;

dylint_linting::declare_late_lint! {
/// This lint is due to David Barsky (@davidbarsky).
Expand Down Expand Up @@ -89,38 +88,44 @@ impl LateLintPass<'_> for AwaitHoldingSpanGuard {
fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
use AsyncGeneratorKind::{Block, Closure, Fn};
if let Some(GeneratorKind::Async(Block | Closure | Fn)) = body.generator_kind {
let body_id = BodyId {
hir_id: body.value.hir_id,
};
let typeck_results = cx.tcx.typeck_body(body_id);
check_interior_types(
cx,
typeck_results
.generator_interior_types
.as_ref()
.skip_binder(),
body.value.span,
);
let def_id = cx.tcx.hir().body_owner_def_id(body.id());
if let Some(generator_layout) = cx.tcx.mir_generator_witnesses(def_id) {
check_interior_types(cx, generator_layout);
}
}
}
}

fn check_interior_types(
cx: &LateContext<'_>,
ty_causes: &[GeneratorInteriorTypeCause<'_>],
span: Span,
) {
for ty_cause in ty_causes {
// smoelius: As part of the upgrade to nightly-2023-10-06, `check_interior_types` was updated based
// on: https://github.com/rust-lang/rust-clippy/commit/0a2d39de2e0b87361432ae695cc84ad74d09972a
fn check_interior_types(cx: &LateContext<'_>, generator: &GeneratorLayout<'_>) {
for (ty_index, ty_cause) in generator.field_tys.iter_enumerated() {
if let Adt(adt, _) = ty_cause.ty.kind() {
let await_points = || {
generator
.variant_source_info
.iter_enumerated()
.filter_map(|(variant, source_info)| {
generator.variant_fields[variant]
.raw
.contains(&ty_index)
.then_some(source_info.span)
})
.collect::<Vec<_>>()
};
if is_tracing_span_guard(cx, adt.did()) {
span_lint_and_note(
span_lint_and_then(
cx,
AWAIT_HOLDING_SPAN_GUARD,
ty_cause.span,
ty_cause.source_info.span,
"this Span guard is held across an 'await' point. Consider using the \
`.instrument()` combinator or the `.in_scope()` method instead",
ty_cause.scope_span.or(Some(span)),
"these are all the await points this ref is held through",
|diag| {
diag.span_note(
await_points(),
"these are all the await points this ref is held through",
);
},
);
}
}
Expand Down
25 changes: 10 additions & 15 deletions examples/general/await_holding_span_guard/ui/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ LL | let _guard = span.enter();
| ^^^^^^
|
note: these are all the await points this ref is held through
--> $DIR/main.rs:20:5
--> $DIR/main.rs:21:11
|
LL | / let _guard = span.enter();
LL | | bar().await;
LL | | }
| |_^
LL | bar().await;
| ^^^^^
= note: `-D await-holding-span-guard` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(await_holding_span_guard)]`

error: this Span guard is held across an 'await' point. Consider using the `.instrument()` combinator or the `.in_scope()` method instead
--> $DIR/main.rs:27:9
Expand All @@ -20,12 +19,10 @@ LL | let _guard = span.entered();
| ^^^^^^
|
note: these are all the await points this ref is held through
--> $DIR/main.rs:27:5
--> $DIR/main.rs:28:11
|
LL | / let _guard = span.entered();
LL | | bar().await;
LL | | }
| |_^
LL | bar().await;
| ^^^^^

error: this Span guard is held across an 'await' point. Consider using the `.instrument()` combinator or the `.in_scope()` method instead
--> $DIR/main.rs:35:13
Expand All @@ -34,12 +31,10 @@ LL | let _guard = span.enter();
| ^^^^^^
|
note: these are all the await points this ref is held through
--> $DIR/main.rs:35:9
--> $DIR/main.rs:36:15
|
LL | / let _guard = span.enter();
LL | | bar().await
LL | | }
| |_____^
LL | bar().await
| ^^^^^

error: aborting due to 3 previous errors

1 change: 1 addition & 0 deletions examples/general/crate_wide_allow/ui/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | #![allow(clippy::assertions_on_constants)]
|
= help: pass `--allow clippy::assertions-on-constants` on the command line
= note: `-D crate-wide-allow` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(crate_wide_allow)]`

error: aborting due to previous error

1 change: 1 addition & 0 deletions examples/general/env_cargo_path/ui/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | let _ = env!("CARGO");
| ^^^^^^^^^^^^^
|
= note: `-D env-cargo-path` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(env_cargo_path)]`

error: this path might not exist in production
--> $DIR/main.rs:3:13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_index::bit_set::BitSet;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::{
mir::{
pretty::write_mir_fn, BasicBlock, Body, Constant, Local, Location, Mutability, Operand,
pretty::write_mir_fn, BasicBlock, Body, ConstOperand, Local, Location, Mutability, Operand,
Place, ProjectionElem, Rvalue, Statement, StatementKind, TerminatorKind,
},
ty,
Expand Down Expand Up @@ -264,7 +264,7 @@ fn collect_locals_and_constants<'tcx>(
mir: &'tcx Body<'tcx>,
path: &[BasicBlock],
args: &[Operand<'tcx>],
) -> (BitSet<Local>, Vec<&'tcx Constant<'tcx>>) {
) -> (BitSet<Local>, Vec<&'tcx Box<ConstOperand<'tcx>>>) {
let mut locals_narrowly = BitSet::new_empty(mir.local_decls.len());
let mut locals_widely = BitSet::new_empty(mir.local_decls.len());
let mut constants = Vec::new();
Expand Down Expand Up @@ -374,7 +374,7 @@ fn is_mut_ref_arg<'tcx>(mir: &'tcx Body<'tcx>, local: Local) -> bool {
(1..=mir.arg_count).contains(&local.into()) && is_mut_ref(mir.local_decls[local].ty)
}

fn is_const_ref(constant: &Constant<'_>) -> bool {
fn is_const_ref(constant: &Box<ConstOperand<'_>>) -> bool {
constant.ty().is_ref()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: error is determined here
LL | Err(VarError::NotPresent)
| ^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `-D non-local-effect-before-error-return` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(non_local_effect_before_error_return)]`

error: call with mutable reference before error return
--> $DIR/main.rs:28:8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: the call is reachable from at least this test
LL | fn foo() {
| ^^^
= note: `-D non-thread-safe-call-in-test` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(non_thread_safe_call_in_test)]`

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: the call is reachable from at least this test
LL | fn env_set_current_dir() {
| ^^^^^^^^^^^^^^^^^^^
= note: `-D non-thread-safe-call-in-test` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(non_thread_safe_call_in_test)]`

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ LL | std::env::set_var("KEY", "VALUE");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D non-thread-safe-call-in-test-pre-expansion` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(non_thread_safe_call_in_test_pre_expansion)]`

error: calling `std::env::set_var` in a test could affect the outcome of other tests
--> $DIR/main.rs:26:9
Expand Down
1 change: 1 addition & 0 deletions examples/general/wrong_serialize_struct_arg/ui/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ note: `serialize_field` call 3 of 3
LL | state.serialize_field("b", &self.b)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `-D wrong-serialize-struct-arg` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(wrong_serialize_struct_arg)]`

error: `serialize_struct` call's `len` argument is 0, but number of `serialize_field` calls is 1
--> $DIR/main.rs:58:33
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | assert_eq!(x, 0);
| ^^^^ help: prefer "expected, actual": `0, x`
|
= note: `-D assert-eq-arg-misordering` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(assert_eq_arg_misordering)]`

error: arguments are "non-const, const", which looks like "actual, expected"
--> $DIR/main.rs:14:9
Expand Down
1 change: 1 addition & 0 deletions examples/restriction/collapsible_unwrap/ui/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | | .try_exists()
| |_____________________^ help: use: `.and_then(|path_buf| path_buf.try_exists())`
|
= note: `-D collapsible-unwrap` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(collapsible_unwrap)]`

error: `unwrap` that could be combined with an `expect`
--> $DIR/main.rs:17:9
Expand Down
1 change: 1 addition & 0 deletions examples/restriction/const_path_join/ui/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | let _ = std::path::Path::new("..").join("target");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `std::path::PathBuf::from("../target")`
|
= note: `-D const-path-join` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(const_path_join)]`

error: path could be constructed from a string literal
--> $DIR/main.rs:5:13
Expand Down
1 change: 1 addition & 0 deletions examples/restriction/derive_opportunity/ui/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | struct Derived;
| ^
|
= note: `-D derive-opportunity` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(derive_opportunity)]`
help: precede with
|
LL + #[derive(Clone, Copy, Hash)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | struct SimpleStruct {
| ^
|
= note: `-D derive-opportunity` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(derive_opportunity)]`
help: precede with
|
LL + #[derive(Default, serde_derive::Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | struct Derived;
| ^
|
= note: `-D derive-opportunity` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(derive_opportunity)]`
help: precede with
|
LL + #[derive(Clone, Copy, Hash)]
Expand Down
1 change: 1 addition & 0 deletions examples/restriction/env_literal/ui/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | let _ = std::env::var("RUSTFLAGS");
|
= help: define a constant `RUSTFLAGS` and use that instead
= note: `-D env-literal` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(env_literal)]`

error: referring to an environment variable with a string literal is error prone
--> $DIR/main.rs:3:26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: items from `std::env` were imported here
LL | use std::env::var;
| ^^^^^^^^^^^^^^^^^^
= note: `-D inconsistent-qualification` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(inconsistent_qualification)]`

error: inconsistent qualification
--> $DIR/main.rs:13:24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | let file = read_to_string(path)?;
|
= help: use a name that is not `dir_builder`, `dir_entry`, `file`, `file_times`, `file_type`, `metadata`, `open_options`, `permissions`, or `read_dir`
= note: `-D misleading-variable-name` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(misleading_variable_name)]`

error: `std::fs` exports a type `File`, which is not the type of `file`
--> $DIR/main.rs:55:9
Expand Down
10 changes: 4 additions & 6 deletions examples/restriction/missing_doc_comment_openai/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(rustc_private)]
#![feature(io_error_other)]
#![feature(let_chains)]
#![warn(unused_extern_crates)]

Expand All @@ -12,7 +11,7 @@ use clippy_utils::{attrs::is_doc_hidden, diagnostics::span_lint_and_then, source
use rustc_ast::AttrKind;
use rustc_hir::{FnSig, Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_span::{BytePos, Span};
use rustc_span::{BytePos, SourceFileAndLine, Span};
use serde::Deserialize;
use std::{
fmt::Write,
Expand Down Expand Up @@ -370,14 +369,13 @@ fn earliest_attr_span(cx: &LateContext<'_>, item: &Item<'_>) -> Span {

fn skip_preceding_line_comments(cx: &LateContext<'_>, mut span: Span) -> Span {
while span.lo() >= BytePos(1) {
let source_file_and_line = cx
let SourceFileAndLine { sf, line } = cx
.sess()
.source_map()
.lookup_line(span.lo() - BytePos(1))
.unwrap();
let lo_prev = source_file_and_line
.sf
.lines(|lines| lines[source_file_and_line.line]);
let lo_prev_relative = sf.lines()[line];
let lo_prev = sf.absolute_position(lo_prev_relative);
let span_prev = span.with_lo(lo_prev);
if snippet_opt(cx, span_prev).map_or(false, |snippet| snippet.starts_with("//")) {
span = span_prev;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | pub fn foo() {}
| ^^^^^^^^^^^^
|
= note: `-D missing-doc-comment-openai` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(missing_doc_comment_openai)]`
help: use the following suggestion from OpenAI
|
LL + /// A doc comment generated by OpenAI.
Expand Down
2 changes: 1 addition & 1 deletion examples/restriction/overscoped_allow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ fn include_trailing_semicolons(cx: &LateContext<'_>, mut span: Span) -> Span {
let Ok(FileLines { file, .. }) = cx.sess().source_map().span_to_lines(span) else {
return span;
};
while span.hi() < file.end_pos {
while span.hi() < file.end_position() {
let next = span.with_hi(span.hi() + BytePos(1));
if !snippet_opt(cx, next).map_or(false, |snip| snip.ends_with(';')) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ help: `allow` could be moved here
LL | pub struct ItemStruct;
| ^
= note: `-D overscoped-allow` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(overscoped_allow)]`

error: `allow` could be moved closer to diagnostic source
--> $DIR/main.rs:10:9
Expand Down
1 change: 1 addition & 0 deletions examples/restriction/overscoped_allow/ui_test/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ LL | #[test]
LL | fn panic() {
| ^
= note: `-D overscoped-allow` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(overscoped_allow)]`
= note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | let _ = git2::Repository::clone(DYLINT_URL, tempfile::tempdir()?.path()
|
= help: consider breaking this up into multiple expressions
= note: `-D question-mark-in-expression` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(question_mark_in_expression)]`

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | Ok(std::path::PathBuf::from(&std::env::var("PWD")?))
|
= help: consider breaking this up into multiple expressions
= note: `-D question-mark-in-expression` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(question_mark_in_expression)]`

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | if !std::fs::read_to_string("Cargo.toml")?.is_empty() {
|
= help: consider breaking this up into multiple expressions
= note: `-D question-mark-in-expression` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(question_mark_in_expression)]`

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo_ref());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `.as_ref().map(TestTrait::trait_foo_ref)`
|
= note: `-D ref-aware-redundant-closure-for-method-calls` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(ref_aware_redundant_closure_for_method_calls)]`

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | .and_then(|mut entries| entries.next())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `.as_mut().and_then(std::iter::Iterator::next)`
|
= note: `-D ref-aware-redundant-closure-for-method-calls` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(ref_aware_redundant_closure_for_method_calls)]`

error: redundant closure
--> $DIR/ref_aware.rs:32:36
Expand Down
Loading

0 comments on commit 4c5ee0c

Please sign in to comment.