From 75cc2d1141e8480609b1f24abf5f4f12f0304a5a Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Tue, 7 Nov 2023 11:54:03 +0100 Subject: [PATCH] syntax-highlighter: add `cargo clippy` to bazel builds and fix warnings (#58096) Co-authored-by: Jean-Hadrien Chabran --- .bazelrc | 4 ++ dev/codeintel-qa/cmd/clear/BUILD.bazel | 1 + .../cmd/clone-and-index/BUILD.bazel | 1 + dev/codeintel-qa/cmd/download/BUILD.bazel | 1 + dev/codeintel-qa/cmd/query/BUILD.bazel | 1 + dev/codeintel-qa/cmd/upload-gcs/BUILD.bazel | 1 + dev/codeintel-qa/cmd/upload/BUILD.bazel | 1 + .../crates/scip-syntax/src/locals.rs | 26 ++++----- .../crates/scip-syntax/src/symbols.rs | 1 - .../crates/scip-treesitter-cli/src/main.rs | 57 ++++++++----------- .../crates/sg-syntax/src/sg_sciptect.rs | 2 +- docker-images/syntax-highlighter/src/main.rs | 1 - internal/cmd/init-sg/BUILD.bazel | 1 + 13 files changed, 50 insertions(+), 48 deletions(-) diff --git a/.bazelrc b/.bazelrc index ad19383a9c9..07f494cb50e 100644 --- a/.bazelrc +++ b/.bazelrc @@ -47,3 +47,7 @@ test:go-verbose-test --test_env=GO_TEST_WRAP_TESTV=1 # .aspect/bazelrc/correctness.bazelrc sets this, but this breaks with a lot of Go external deps, so # we instead disable it. common --noincompatible_disallow_empty_glob + +# Ensure clippy runs for rust targets +build --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect +build --output_groups=+clippy_checks diff --git a/dev/codeintel-qa/cmd/clear/BUILD.bazel b/dev/codeintel-qa/cmd/clear/BUILD.bazel index 6115ce23a57..48bd0310caf 100644 --- a/dev/codeintel-qa/cmd/clear/BUILD.bazel +++ b/dev/codeintel-qa/cmd/clear/BUILD.bazel @@ -23,6 +23,7 @@ go_binary( go_cross_binary( name = "clear-darwin-arm64", platform = "@io_bazel_rules_go//go/toolchain:darwin_arm64", + tags = ["manual"], target = ":clear", visibility = ["//testing:__pkg__"], ) diff --git a/dev/codeintel-qa/cmd/clone-and-index/BUILD.bazel b/dev/codeintel-qa/cmd/clone-and-index/BUILD.bazel index acf24fb927e..e8a7d360c13 100644 --- a/dev/codeintel-qa/cmd/clone-and-index/BUILD.bazel +++ b/dev/codeintel-qa/cmd/clone-and-index/BUILD.bazel @@ -23,6 +23,7 @@ go_binary( go_cross_binary( name = "clone-and-index-darwin-arm64", platform = "@io_bazel_rules_go//go/toolchain:darwin_arm64", + tags = ["manual"], target = ":clone-and-index", visibility = ["//testing:__pkg__"], ) diff --git a/dev/codeintel-qa/cmd/download/BUILD.bazel b/dev/codeintel-qa/cmd/download/BUILD.bazel index 29d9a0192a9..bfc518ff8ad 100644 --- a/dev/codeintel-qa/cmd/download/BUILD.bazel +++ b/dev/codeintel-qa/cmd/download/BUILD.bazel @@ -24,6 +24,7 @@ go_binary( go_cross_binary( name = "download-darwin-arm64", platform = "@io_bazel_rules_go//go/toolchain:darwin_arm64", + tags = ["manual"], target = ":download", visibility = ["//testing:__pkg__"], ) diff --git a/dev/codeintel-qa/cmd/query/BUILD.bazel b/dev/codeintel-qa/cmd/query/BUILD.bazel index 99f1264ddb2..7947bc456c7 100644 --- a/dev/codeintel-qa/cmd/query/BUILD.bazel +++ b/dev/codeintel-qa/cmd/query/BUILD.bazel @@ -30,6 +30,7 @@ go_binary( go_cross_binary( name = "query-darwin-arm64", platform = "@io_bazel_rules_go//go/toolchain:darwin_arm64", + tags = ["manual"], target = ":query", visibility = ["//testing:__pkg__"], ) diff --git a/dev/codeintel-qa/cmd/upload-gcs/BUILD.bazel b/dev/codeintel-qa/cmd/upload-gcs/BUILD.bazel index dd295643d9c..eadcb2904c3 100644 --- a/dev/codeintel-qa/cmd/upload-gcs/BUILD.bazel +++ b/dev/codeintel-qa/cmd/upload-gcs/BUILD.bazel @@ -23,6 +23,7 @@ go_binary( go_cross_binary( name = "upload-gcs-darwin-arm64", platform = "@io_bazel_rules_go//go/toolchain:darwin_arm64", + tags = ["manual"], target = ":upload-gcs", visibility = ["//testing:__pkg__"], ) diff --git a/dev/codeintel-qa/cmd/upload/BUILD.bazel b/dev/codeintel-qa/cmd/upload/BUILD.bazel index 1252f79f76a..fd66936b853 100644 --- a/dev/codeintel-qa/cmd/upload/BUILD.bazel +++ b/dev/codeintel-qa/cmd/upload/BUILD.bazel @@ -24,6 +24,7 @@ go_binary( go_cross_binary( name = "upload-darwin-arm64", platform = "@io_bazel_rules_go//go/toolchain:darwin_arm64", + tags = ["manual"], target = ":upload", visibility = ["//testing:__pkg__"], ) diff --git a/docker-images/syntax-highlighter/crates/scip-syntax/src/locals.rs b/docker-images/syntax-highlighter/crates/scip-syntax/src/locals.rs index 621f0eb1f30..03ef5b1bc9f 100644 --- a/docker-images/syntax-highlighter/crates/scip-syntax/src/locals.rs +++ b/docker-images/syntax-highlighter/crates/scip-syntax/src/locals.rs @@ -195,7 +195,7 @@ impl<'a> Scope<'a> { } ReassignmentBehavior::OldestIsDefinition => { if let Some(above) = declarations_above - .into_iter() + .iter_mut() .rev() .find(|x| x.contains_key(lvalue.identifier)) { @@ -540,9 +540,9 @@ mod test { #[test] fn test_can_do_go() -> Result<()> { - let mut config = crate::languages::get_local_configuration(BundledParser::Go).unwrap(); + let config = crate::languages::get_local_configuration(BundledParser::Go).unwrap(); let source_code = include_str!("../testdata/locals.go"); - let doc = parse_file_for_lang(&mut config, source_code)?; + let doc = parse_file_for_lang(config, source_code)?; let dumped = snapshot_syntax_document(&doc, source_code); insta::assert_snapshot!(dumped); @@ -552,9 +552,9 @@ mod test { #[test] fn test_can_do_nested_locals() -> Result<()> { - let mut config = crate::languages::get_local_configuration(BundledParser::Go).unwrap(); + let config = crate::languages::get_local_configuration(BundledParser::Go).unwrap(); let source_code = include_str!("../testdata/locals-nested.go"); - let doc = parse_file_for_lang(&mut config, source_code)?; + let doc = parse_file_for_lang(config, source_code)?; let dumped = snapshot_syntax_document(&doc, source_code); insta::assert_snapshot!(dumped); @@ -564,9 +564,9 @@ mod test { #[test] fn test_can_do_functions() -> Result<()> { - let mut config = crate::languages::get_local_configuration(BundledParser::Go).unwrap(); + let config = crate::languages::get_local_configuration(BundledParser::Go).unwrap(); let source_code = include_str!("../testdata/funcs.go"); - let doc = parse_file_for_lang(&mut config, source_code)?; + let doc = parse_file_for_lang(config, source_code)?; let dumped = snapshot_syntax_document(&doc, source_code); insta::assert_snapshot!(dumped); @@ -576,9 +576,9 @@ mod test { #[test] fn test_can_do_perl() -> Result<()> { - let mut config = crate::languages::get_local_configuration(BundledParser::Perl).unwrap(); + let config = crate::languages::get_local_configuration(BundledParser::Perl).unwrap(); let source_code = include_str!("../testdata/perl.pm"); - let doc = parse_file_for_lang(&mut config, source_code)?; + let doc = parse_file_for_lang(config, source_code)?; let dumped = snapshot_syntax_document(&doc, source_code); insta::assert_snapshot!(dumped); @@ -588,9 +588,9 @@ mod test { #[test] fn test_can_do_matlab() -> Result<()> { - let mut config = crate::languages::get_local_configuration(BundledParser::Matlab).unwrap(); + let config = crate::languages::get_local_configuration(BundledParser::Matlab).unwrap(); let source_code = include_str!("../testdata/locals.m"); - let doc = parse_file_for_lang(&mut config, source_code)?; + let doc = parse_file_for_lang(config, source_code)?; let dumped = snapshot_syntax_document(&doc, source_code); insta::assert_snapshot!(dumped); @@ -600,9 +600,9 @@ mod test { #[test] fn test_can_do_java() -> Result<()> { - let mut config = crate::languages::get_local_configuration(BundledParser::Java).unwrap(); + let config = crate::languages::get_local_configuration(BundledParser::Java).unwrap(); let source_code = include_str!("../testdata/locals.java"); - let doc = parse_file_for_lang(&mut config, source_code)?; + let doc = parse_file_for_lang(config, source_code)?; let dumped = snapshot_syntax_document(&doc, source_code); insta::assert_snapshot!(dumped); diff --git a/docker-images/syntax-highlighter/crates/scip-syntax/src/symbols.rs b/docker-images/syntax-highlighter/crates/scip-syntax/src/symbols.rs index 8260f8fb563..4eef762f337 100644 --- a/docker-images/syntax-highlighter/crates/scip-syntax/src/symbols.rs +++ b/docker-images/syntax-highlighter/crates/scip-syntax/src/symbols.rs @@ -1,5 +1,4 @@ use anyhow::Result; -use bitvec::prelude::*; use protobuf::Enum; use scip::types::{symbol_information, Descriptor, Document, Occurrence, SymbolInformation}; use scip_treesitter::types::PackedRange; diff --git a/docker-images/syntax-highlighter/crates/scip-treesitter-cli/src/main.rs b/docker-images/syntax-highlighter/crates/scip-treesitter-cli/src/main.rs index bcaf7330a32..7b4dca7f69a 100644 --- a/docker-images/syntax-highlighter/crates/scip-treesitter-cli/src/main.rs +++ b/docker-images/syntax-highlighter/crates/scip-treesitter-cli/src/main.rs @@ -1,12 +1,12 @@ use clap::{Parser, Subcommand, ValueEnum}; use indicatif::{ProgressBar, ProgressStyle}; -use protobuf::{CodedInputStream, Message}; +use protobuf::Message; use scip::types::Document; use scip_syntax::{get_locals, get_symbols}; use scip_treesitter_languages::parsers::BundledParser; use anyhow::Result; -use std::{fs::File, io::BufReader, path::PathBuf}; +use std::path::PathBuf; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -28,10 +28,10 @@ enum AnalysisMode { impl AnalysisMode { fn locals(self) -> bool { - return self == AnalysisMode::Locals || self == AnalysisMode::Full; + self == AnalysisMode::Locals || self == AnalysisMode::Full } fn globals(self) -> bool { - return self == AnalysisMode::Globals || self == AnalysisMode::Full; + self == AnalysisMode::Globals || self == AnalysisMode::Full } } @@ -159,7 +159,7 @@ fn index_command( bar.finish(); - eprintln!(""); + eprintln!(); eprintln!( "Writing index for {} documents into {}", @@ -193,13 +193,10 @@ fn index_content(contents: Vec, parser: BundledParser, options: &Options) -> } } - return Ok(document); + Ok(document) } -fn write_message_to_file

( - path: P, - msg: impl protobuf::Message, -) -> Result<(), Box> +fn write_message_to_file

(path: P, msg: impl Message) -> Result<(), Box> where P: AsRef, { @@ -213,44 +210,40 @@ where Ok(()) } -fn read_index_from_file(file: PathBuf) -> scip::types::Index { - let mut candidate_idx = scip::types::Index::new(); - let candidate_f = File::open(file).unwrap(); - - let mut reader = BufReader::new(candidate_f); - let mut cis = CodedInputStream::from_buf_read(&mut reader); - - candidate_idx.merge_from(&mut cis).unwrap(); - return candidate_idx; -} - #[cfg(test)] mod tests { - use crate::read_index_from_file; use assert_cmd::cargo::cargo_bin; use assert_cmd::prelude::*; + use protobuf::{CodedInputStream, Message}; use std::collections::HashMap; + use std::fs::File; + use std::io::BufReader; + use std::path::Path; use std::process::Command; use std::{env::temp_dir, path::PathBuf}; lazy_static::lazy_static! { static ref BINARY_LOCATION: PathBuf = { - let mut c: PathBuf; match std::env::var("SCIP_CLI_LOCATION") { - Ok(va) => { - c = { - std::env::current_dir().unwrap().join(va) - } - } - _ => c = cargo_bin("scip-treesitter-cli") + Ok(va) => std::env::current_dir().unwrap().join(va), + _ => cargo_bin("scip-treesitter-cli"), } - - c }; } use scip_treesitter::snapshot::{dump_document_with_config, EmitSymbol, SnapshotOptions}; + fn read_index_from_file(file: PathBuf) -> scip::types::Index { + let mut candidate_idx = scip::types::Index::new(); + let candidate_f = File::open(file).unwrap(); + + let mut reader = BufReader::new(candidate_f); + let mut cis = CodedInputStream::from_buf_read(&mut reader); + + candidate_idx.merge_from(&mut cis).unwrap(); + candidate_idx + } + fn snapshot_syntax_document(doc: &scip::types::Document, source: &str) -> String { dump_document_with_config( doc, @@ -284,7 +277,7 @@ mod tests { } } - fn prepare(temp: &PathBuf, files: &HashMap) { + fn prepare(temp: &Path, files: &HashMap) { for (path, contents) in files.iter() { let file_path = temp.join(path); write_file(&file_path, contents); diff --git a/docker-images/syntax-highlighter/crates/sg-syntax/src/sg_sciptect.rs b/docker-images/syntax-highlighter/crates/sg-syntax/src/sg_sciptect.rs index f12d1cca481..ef8f44be7b6 100644 --- a/docker-images/syntax-highlighter/crates/sg-syntax/src/sg_sciptect.rs +++ b/docker-images/syntax-highlighter/crates/sg-syntax/src/sg_sciptect.rs @@ -372,7 +372,7 @@ impl<'a> DocumentGenerator<'a> { Some(char) => char.0, None => { line_contents.chars().count() - - (if line_contents.ends_with("\n") { 1 } else { 0 }) + - (if line_contents.ends_with('\n') { 1 } else { 0 }) } }; diff --git a/docker-images/syntax-highlighter/src/main.rs b/docker-images/syntax-highlighter/src/main.rs index 35aac508288..81d80c3fed6 100644 --- a/docker-images/syntax-highlighter/src/main.rs +++ b/docker-images/syntax-highlighter/src/main.rs @@ -7,7 +7,6 @@ use std::path; use protobuf::Message; use rocket::serde::json::{json, Json, Value as JsonValue}; -use scip_syntax::get_globals; use scip_treesitter_languages::parsers::BundledParser; use serde::Deserialize; use sg_syntax::{ScipHighlightQuery, SourcegraphQuery}; diff --git a/internal/cmd/init-sg/BUILD.bazel b/internal/cmd/init-sg/BUILD.bazel index 4a55b417594..411d5d96969 100644 --- a/internal/cmd/init-sg/BUILD.bazel +++ b/internal/cmd/init-sg/BUILD.bazel @@ -20,6 +20,7 @@ go_binary( go_cross_binary( name = "init-sg-darwin-arm64", platform = "@io_bazel_rules_go//go/toolchain:darwin_arm64", + tags = ["manual"], target = ":init-sg", visibility = ["//testing:__pkg__"], )